1

I have this array of voters:

let voters = [
    {name:'Bob' , age: 30, voted: true},
    {name:'Jake' , age: 32, voted: true},
    {name:'Kate' , age: 25, voted: false},
    {name:'Sam' , age: 20, voted: false},
    {name:'Phil' , age: 21, voted: true},
    {name:'Ed' , age:55, voted:true},
    {name:'Tami' , age: 54, voted:true},
    {name: 'Mary', age: 31, voted: false},
    {name: 'Becky', age: 43, voted: false},
    {name: 'Joey', age: 41, voted: true},
    {name: 'Jeff', age: 30, voted: true},
    {name: 'Zack', age: 19, voted: false}
];

That I need to count how many voted (true).

I was able to find it using a for loop, but I'm learning the reduce method, and would like to apply it here but can't seem to figure out how.

// works perfectly
function totalVotes(voters){
      let total = 0;
      for (i=0; i < voters.length; i++){
        if (voters[i].voted === true) {
          total++
        }
      }
      return total
    }
function totalVotes2(voters){
      voters.reduce(function (contador, item){
        if (item.voted === true){
          console.log(item)
          console.log(contador)
          //cant figure out how to make it count
          }
          // and how to return it
      }, 0)
    }

1 Answer 1

2

You can set the accumulator's initial value to 0 and add 1 to it if the current element's voted property is true and add 0 otherwise.

let voters = [
    {name:'Bob' , age: 30, voted: true},
    {name:'Jake' , age: 32, voted: true},
    {name:'Kate' , age: 25, voted: false},
    {name:'Sam' , age: 20, voted: false},
    {name:'Phil' , age: 21, voted: true},
    {name:'Ed' , age:55, voted:true},
    {name:'Tami' , age: 54, voted:true},
    {name: 'Mary', age: 31, voted: false},
    {name: 'Becky', age: 43, voted: false},
    {name: 'Joey', age: 41, voted: true},
    {name: 'Jeff', age: 30, voted: true},
    {name: 'Zack', age: 19, voted: false}
];
let res = voters.reduce((acc, {voted})=>acc + (voted ? 1 : 0), 0);
console.log(res);

You could also make use of the fact that true can be coerced to 1 and false can be coerced to 0 in JavaScript, obviating the need for the ternary operator.

let voters = [
    {name:'Bob' , age: 30, voted: true},
    {name:'Jake' , age: 32, voted: true},
    {name:'Kate' , age: 25, voted: false},
    {name:'Sam' , age: 20, voted: false},
    {name:'Phil' , age: 21, voted: true},
    {name:'Ed' , age:55, voted:true},
    {name:'Tami' , age: 54, voted:true},
    {name: 'Mary', age: 31, voted: false},
    {name: 'Becky', age: 43, voted: false},
    {name: 'Joey', age: 41, voted: true},
    {name: 'Jeff', age: 30, voted: true},
    {name: 'Zack', age: 19, voted: false}
];
let res = voters.reduce((acc, {voted})=>acc + voted, 0);
console.log(res);

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

6 Comments

Dear @hev1, thanks! This works but now I don't understand how does {voted} captures the value of that KVP. Could you help me figure that out?
I just used {age}, with the accumulator set to zero, and it added up all ages, as expected. But I still don't understand how could {age} pick the value of age:number
ooooooooooooooooooooooohhhhhhhhhhhhhhhhhhhhh! : ) Thanks!
@LeonardoNobreGhiggino No problem. Does that answer your question?
|

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.