0

I have simple JavaScript question.

I have an array (xyz) of 9 objects (Cell), each with 2 properties (e,v).

Is there any easy way to check if all the values of one of the properties are false?

Something like:

var myArray = xyz[];

if(all myArray.e==false){
    do this;
}

Thanks :D

Array[9]
0:Cell
 e:false
 v:"x"
1:Cell
2:Cell
3:Cell
4:Cell
...etc
0

6 Answers 6

1

You can use JavaScript Array every() Method

var myArray = [xyz];
if (myArray.every(obj => !obj.e)) {
   do this;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I agree. Can you add some explanation or a snippet?
Thanks, this looks very simple and useful.
0

You could do this with the reduce function.

Try:

const areAllFalse = !myArray.reduce((prev, cur) => prev || cur, false)

Check out the MDN docs for reduce for more info on how it works.

Comments

0

You should do

let areAllFalse = true;
for (let obj in myArray){
    if (e.v) {
       areAllFalse = false;
       break;
    }
}
if (areAllFalse){
    // Do stuff
}

Basically:

  • You instantiate a variable (in this snippet is called areAllFalse) to true (you are assuming that all the elements will be false)
  • Then you cycle them all
  • If at least one is true, you change the value of that variable and exit from the cycle as it's useles to continue
  • If the variable areAllFalse is true, every element in the array is false, so you can execute your code

This will work in every programming language (with the obvious syntax changes)

I hope I helped you :)

Comments

0

There's a few different ways you could approach this. One way would be by using every. (MDN Docs)

function myTest(thisItem) {
    return !thisItem.e;
}

var myArray = [
    {e: false, v: 111},
    {e: false, v: 222},
    {e: false, v: 333}
];

var output = myArray.every(myTest);

console.log(output);
// expected output: true

Comments

0

You have multiple options to do that, like

  1. Normal for loop
  2. ForEach loop
  3. Or any looping mechanism using that you can check whether it contains a false property.

But for short answer I think you can use findIndex that will return index if it matches condition: Example :

if(xyz.findIndex(k => k.e== false) > -1){
console.log("it contains false value also");
}

Comments

0

You can use array#every with to check for your condition to be true for all the objects in your array. Use Object#keys to iterate over keys in an object and check for false value using array#some.

var arr = [{e:false, v:'x'}, {e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'}],
    result = arr.every(o => Object.keys(o).some(k => !o[k]));
console.log(result);

To check for e property in your object, you should first check if e key exists in the object then check if its value is false.

var arr = [{e:false, v:'x'}, {e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'},{e:false, v:'x'}],
    result = arr.every(o => 'e' in o && !o.e);
console.log(result);

3 Comments

Thanks, this looks nice and elegant, but is it checking for arr.e values or for any key values ? If I have var arr = [{e:true, v:false}, {e:false, v:false}] result will still be true.
It is checking for any value in object. Do you want to just check for e value?
Yes only one of the properties (e), the rest can all be true or false, or any other value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.