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 }
]
}
];
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 aforEachwould not be, at least theoretically, more performatic? I say this because withforEachit is possible to stop the loop as soon as a value does not satisfy the condition, dispensing with analyzing everything. Worth!– Sam
@Sam, the
everyfor the scan so the first predicate evaluatesfalse(otherwise, assuming everyone returnstrue, scan everything as expected). This behavior is predicted by the specification. See here.– Luiz Felipe
Ah, cool. By logic, he should stop even, otherwise it would be meaningless and the implementation of this method would be "dumb" rsrs... thanks!
– Sam
@Sam I think with
forEachis 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 ofevery,some, etc. In fact the alternative would be aforsimple, 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
@hkotsubo Ah, cool. Only that the
forEachyou can stop with areturn, of course, within an exception (if). Really aforsimple would be better even.– Sam