0

I'm using vue-cli and I'm trying to figure out how I can get the total numbers of active in Tasks array.

Tasks: [ 
            {name: 'Sam',  available: [{active: 'yes', day:'Mon'},{active: 'yes', day:'Tues'}]},
            {name: 'Yoko', available: [{active: 'yes', day:'Mon'}]},
            {name: 'Alec', available: [{active: 'yes', day:'Wed'},{active: 'yes', day:'Thurs'}]},
            {name: 'Pat',  available: [{active: 'yes', day:'Tues'},{active: 'yes', day:'Thurs'} ]}
       ]
 
Expected output:
Total: 7

Any help would be great, thanks!

1 Answer 1

2

You need iterate through tasks array, filter active items and sum it with reduce method:

const tasks = [ 
  {name: 'Sam',  available: [{active: 'yes', day:'Mon'},{active: 'yes', day:'Tues'}]},
  {name: 'Yoko', available: [{active: 'yes', day:'Mon'}]},
  {name: 'Alec', available: [{active: 'yes', day:'Wed'},{active: 'yes', day:'Thurs'}]},
  {name: 'Pat',  available: [{active: 'yes', day:'Tues'},{active: 'yes', day:'Thurs'} ]}
];

const total = tasks.reduce((sum, task) => {
    return sum + (task.available.filter(item => item.active === 'yes')).length
}, 0)
Sign up to request clarification or add additional context in comments.

Comments

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.