How can I get the indices where array entries match a particular condition?
Say, want to match device = ‘1’…
[
{ device: '1', type: 'a' },
{ device: '2', type: 'b' },
{ device: '3', type: 'c' },
{ device: '1', type: 'd' }
]
The returned result should be array [0,3].
Does JavaScript have a findIndexAll() or something like that?
2
Answers
You can create a function to do this using a combination of "map" and "indexOf", like this:
You can use reduce to push the indices of the matching entries into an accumulator array: