In the following object called ‘task’, the door
property could arbitrary each time:
var task = {};
task["123"] = [{"door":""}, {"door":""}, {"door":""}, {"door":""}];
task["456"] = [{"door":""}, {"door":"close"}, {"door":""}, {"door":""}];
task["789"] = [{"door":""}, {"door":""}, {"door":""}, {"door":""}];
Let’s say we need to find if there is an entry in the task object which has an array item with a property not equal to "". So in the above, there is only one such entry and its value is "close".
One could use nested for loops which bail out as soon as a non empty value is found. How else could one do it? (maybe using LINQ to create shorter code?)
2
Answers
LINQ belongs to C# not JS. But you can use ES6 syntax e. g. a
.some()
after a.flat()
if you want to detect if there’s adoor !== ""
:Could be done with several
Object::values
andArray::some
in a chain:One crazy way would be to use a regex if you need the smallest code. It’s possible to regex only the arrays which is actually faster than flatting the whole object:
And a benchmark: