user11624254
user11624254

Reputation:

How to check if all the values of a key inside an object array is 0?

I have below object inside an array

[
    {
        "event":"a",
        "count":0
    },
    {
        "event":"b",
        "count":0
    },
    {
        "event":"c",
        "count":0
    }
]

I need to check if all values of count is zero.

I have tried below code

Object.values(alarmTypeCount).every(count => count === 0)

it returns false every time

Upvotes: 0

Views: 91

Answers (5)

yuval.bl
yuval.bl

Reputation: 5074

There are several options using Array.prototype methods, here's a summary, inlcuding some of the options mentioned here by others:

Using every

const isAllZeros = alarmTypeCount.every(a => a.count === 0)

Using some

const isAllZeros = !alarmTypeCount.some(a => a.count !== 0)

Using find

const isAllZeros = !alarmTypeCount.find(a => a.count !== 0)

Using findIndex

const isAllZeros = alarmTypeCount.findIndex(a => a.count !== 0) === -1

Using filter

const isAllZeros = alarmTypeCount.filter(a => a.count !== 0).length === 0

Upvotes: 1

Nick Parsons
Nick Parsons

Reputation: 50884

Firstly, Object.values() on an array is redundant as it will simply just give you an array of the elements (objects in your case) within your array, which is what your array is anyway. count in your example represents a given object in your array. You need to access the object count property like so:

alarmTypeCount.every(obj => obj.count === 0)

Or, you can do it through destructuring assignment:

alarmTypeCount.every(({count}) => count === 0)

See example below:

let alarmTypeCount = [{"event":"a", "count":0}, {"event":"b", "count":0}, {"event":"c", "count":0}];

console.log(alarmTypeCount.every(obj => obj.count === 0));

Upvotes: 2

Rohit.007
Rohit.007

Reputation: 3502

You can check with the filter method

let array = [
    {
        "event":"a",
        "count":0
    },
    {
        "event":"b",
        "count":0
    },
    {
        "event":"c",
        "count":0
    }
]

let newArray = array.filter((obj)=>{
  return obj.count > 0;
});

if(newArray.length == 0){
  console.log("All the elements are zero");
}else{
  console.log(newArray.length + " elements are non zero");
}

Upvotes: 0

nivendha
nivendha

Reputation: 837

You need to destructure your argument of 'every' function

Object.values(alarmTypeCount).every(({ count }) => count === 0)

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386746

You need a destructuring of the object to get the wanted property.

var alarmTypeCount = [{ event: "a", count: 0 }, { event: "b", count: 0 }, { event: "c", count: 0 }],
    allCountsZero = Object.values(alarmTypeCount).every(({ count }) => count === 0);

console.log(allCountsZero);

Or take the object with a property.

var alarmTypeCount = [{ event: "a", count: 0 }, { event: "b", count: 0 }, { event: "c", count: 0 }],
    allCountsZero = Object.values(alarmTypeCount).every(o => o.count === 0);

console.log(allCountsZero);

Upvotes: 5

Related Questions