Check that all sub-rray items are in "false" status

Asked

Viewed 75 times

0

I have an array(parent) and within it I have two more arrays. The child arrays exist a property status. I would like to know how I can validate that all items in the array are with the property status as false.

let materials = [
  {
    group: 'A',
    data: [
      { id: 1, name: 'PC Dell', materialGroup: 'A', amount: 1, status: false },
      { id: 2, name: 'Mouse', materialGroup: 'A', amount: 2, status: false }
    ]
  },
  {
    group: 'B',
    data: [
      { id: 3, name: 'Teclado', materialGroup: 'B', amount: 1, status: false }
    ]
  }
];

1 answer

6


The method Array.prototype.every checks whether all elements of an array satisfy a predicate. See the example:

const arr1 = [11, 12, 13, 14, 15];
const test1 = arr1.every((num) => num > 10); // verifica se cada elemento é maior que 10
console.log(test1); //=> true

const arr2 = [9, 12, 13, 14, 15];
const test2 = arr2.every((num) => num > 10); // verifica se cada elemento é maior que 10
console.log(test2); //=> false

So you can use this method.

Just note that, as you have a data set with "depth" of two arrays, you will have to nest two calls to every. Sort of like this:

const materials = [
  {
    group: 'A',
    data: [
      { id: 1, name: 'PC Dell', materialGroup: 'A', amount: 1, status: false },
      { id: 2, name: 'Mouse', materialGroup: 'A', amount: 2, status: false }
    ]
  },
  {
    group: 'B',
    data: [
      { id: 3, name: 'Teclado', materialGroup: 'B', amount: 1, status: false }
    ]
  }
];

const result = materials.every((material) =>
  material.data.every((entry) => !entry.status)
);
console.log(result);


Not much to do with the question, but notice:

  • every checks if the predicate is true for all the array elements.
  • some checks if the predicate is true for at least one of the array elements.
  • 1

    And Felipe, blz? Man, ask me a question: I thought it was cool every, but I imagine that it will analyze each item of the array to verify the values, no? Will a forEach would not be, at least theoretically, more performatic? I say this because with forEach it is possible to stop the loop as soon as a value does not satisfy the condition, dispensing with analyzing everything. Worth!

  • 2

    @Sam, the every for the scan so the first predicate evaluates false (otherwise, assuming everyone returns true, scan everything as expected). This behavior is predicted by the specification. See here.

  • 1

    Ah, cool. By logic, he should stop even, otherwise it would be meaningless and the implementation of this method would be "dumb" rsrs... thanks!

  • 1

    @Sam I think with forEach is worse because according to documentation: "There is no way to stop or break a foreach() loop other than by Throwing an Exception. If you need such behavior, the foreach() method is the Wrong tool" - and soon after it is recommended the use of every, some, etc. In fact the alternative would be a for simple, that yes more performatic for not having the calls of callbacks: https://jsbench.me/7ukt1mbwe0/1 (but for small arrays the difference is negligible)

  • @hkotsubo Ah, cool. Only that the forEach you can stop with a return, of course, within an exception (if). Really a for simple would be better even.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.