1

I have an array of objects that I'm trying to get the output to display properly after using a filter and reduce.

It's outputting the data properly but not in the format I would like. Any input is much appreciated.

CURRENT OUTPUT

{
 5: 12,
 6: 15,
 7: 12,
 ...
}

EXPECTED OUTPUT

{
 session5-count: 12,
 session6-count: 15,
 session7-count: 12,
 ...
}

I'm sure this is something very simple and I haven't had lunch so could be blocking my thought process.

here is my formula and sample data

UPDATED FUNCTION

Thanks to alex-g & zfrisch

const sessionsArray = [
    { session: '5', status: 'inactive' },
    { session: '5', status: 'inactive' },
    { session: '5', status: 'inactive' },
    { session: '5', status: 'active' },
    { session: '5', status: 'active' },
    { session: '5', status: 'active' },
    { session: '5', status: 'active' },
    { session: '5', status: 'active' },
    { session: '5', status: 'active' },
    { session: '5', status: 'active' },
    { session: '5', status: 'active' },
    { session: '5', status: 'active' },
    { session: '6', status: 'active' },
    { session: '6', status: 'active' },
    { session: '7', status: 'inactive' },
    { session: '7', status: 'inactive' },
    { session: '7', status: 'inactive' },
    ];

const filteredSessions  = sessionsArray
    .reduce( (acc, {session, status}) => {

        if(status != 'active'){
           acc["session-" + session ] = 0
        }else{           
            acc["session-" + session ] =
            acc["session-" + session ] + 1 || 1
        } 
        
        return acc
    }, {}) 

console.log(filteredSessions )

I'm trying to follow along and use THIS S.O. ANSWER to help format my output

1 Answer 1

2

You can use template strings to get the desired key format. Like this:

const sessionsArray = [
{ session: '5', status: 'inactive' },
{ session: '5', status: 'inactive' },
{ session: '5', status: 'inactive' },
{ session: '5', status: 'active' },
{ session: '5', status: 'active' },
{ session: '5', status: 'active' },
{ session: '5', status: 'active' },
{ session: '5', status: 'active' },
{ session: '5', status: 'active' },
{ session: '5', status: 'active' },
{ session: '5', status: 'active' },
{ session: '5', status: 'active' },
{ session: '6', status: 'active' },
{ session: '6', status: 'active' },
{ session: '7', status: 'inactive' },
{ session: '7', status: 'active' },
{ session: '7', status: 'active' },
];

const filteredSessions = sessionsArray
    .filter(workshop => workshop.status === 'active')
    .reduce( (acc, { session }) => {
        
        acc[`session${session}-count`] = acc[`session${session}-count`] + 1 || 1;
        
        return acc;
    }, {});

console.log(filteredSessions)

Sign up to request clarification or add additional context in comments.

5 Comments

Exactly what I needed!
as an aside, you can use destructuring to just grab the one property you need. A.e. .reduce((acc, {session}) =>
Thats true @zfrisch . Thinking about this now. This does not account for if a session has all entries as 'inactive'. If I remove the filter function. Can I add a condition to return 0 within the reduce function?
@cpt-crunchy .reduce(acc, {session, status}) => { if(status != "active") return acc;
I was just testing that out. Thanks @zfrisch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.