4

I've written a method to calculate the number of objects in an array which have 'enabled' set to 'true'.

I'm adding 1 to counter each time it finds an object in my array which has 'enabled' set to 'true'.

How could I achieve this without using 'counter' variable and using reduce or filter instead??

Here's my code:

function getCount() {               
    const arr =[[{ "enabled": true }], [{ "enabled": false}, {"enabled": true}]];                       
    var count = 0;
    arr.forEach(function(ar){
        ar.forEach(function(obj){
            if(obj.enabled) {
                count++;
            }
        })
    });
    return count;           
}
2
  • What is obj.seatMapAvailable? Did you mean obj.enabled? Commented Oct 31, 2018 at 13:00
  • yes, that's true, corrected it now! I want to use reduce from ES6. Commented Oct 31, 2018 at 13:10

4 Answers 4

9

Have a look below, I've added a comment:

[].concat(...arr) /* flatten the array */
.filter(item => item.enabled) /* return only enabled: true */
.length /* get the count */

const arr = [
  [{
    "enabled": true
  }],
  [{
    "enabled": false
  }, {
    "enabled": true
  }]
];
var enabledCount = [].concat(...arr).filter(item => item.enabled).length
console.log(enabledCount)

Or you can use reduce, if you want

const arr = [
  [{
    "enabled": true
  }],
  [{
    "enabled": false
  }, {
    "enabled": true
  }]
];

var enabledCount = arr.reduce(
  (accumulator, currentValue) => accumulator.concat(currentValue), []
).filter(item => item.enabled).length

console.log(enabledCount)

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

2 Comments

I want to use reduce from ES6
I've added a reduce example
3

Using a helper reduce function gives the simplest implementation in my opinion:

const arr =[
 [
     {"enabled": true},
     {"enabled": true}
 ],
 [
     {"enabled": false}, 
     {"enabled": true},
     {"enabled": true}
 ]
]; 

// Helper function to count inner arrays
const r = (i) => i.reduce( (p, c) => c.enabled ? p = p + 1 : p, 0)

const count = arr.reduce( (p, c) => p + r(c), 0) // Output: 4

1 Comment

if your [arr] has properties ( ike count): THEN arr.reduce((p, c) => (c.prop.enabled ? p + +c.count : p), 0)
1

Would something like this work?

const arr =[[{ "enabled": true }], [{ "enabled": false}, {"enabled": true}]];               
const enabledArray = arr.map(function(item) {
    return item.filter(function(subItem){
        return subItem.enabled === true;
    })
})
const enabledItems = enabledArray.length;

Comments

1

You could do:

const arr = [[{ "enabled": true }], [{ "enabled": false }, { "enabled": true }]];

console.log(
    arr.reduce(
    // Flatten the array of arrays
        (acc, curVal) => acc.concat(curVal), []         
    ).filter(
    // Filter so you have an array with 
    //       objects that have 'enable': true
        (obj) => Object.is(obj['enabled'], true))
    // and then return the length of it
    .length
);

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.