I am trying to check if all the objects in an array is equal to false ,if yes then return true in react. my array of objects looks like bellow
[{"filter":"ASIA","key":0,"isChecked":false},{"filter":"INDIA","key":1,"isChecked":false},{"filter":"MANDYA","key":2,"isChecked":false},{"filter":"GOA","key":3,"isChecked":false},{"filter":"KERALA","key":4,"isChecked":false}]
I have tried bellow code using both every and some:
let check = arr.some(element => element.isChecked === false);
and
let check = arr.every(element => element.isChecked === false);
both the above statements returns false
CodePudding user response:
let check = arr.every(element => element.isChecked === false);
should be correct. You want to check and assert that every element in the array meets a condition.
It could be simplified a bit to arr.every(({ isChecked }) => !isChecked);
since isChecked
is already a boolean value.
const arr = [{"filter":"ASIA","key":0,"isChecked":false},{"filter":"INDIA","key":1,"isChecked":false},{"filter":"MANDYA","key":2,"isChecked":false},{"filter":"GOA","key":3,"isChecked":false},{"filter":"KERALA","key":4,"isChecked":false}];
const check = arr.every(({ isChecked }) => !isChecked);
console.log(check);