When delving into ES6, I came across a situation where I needed to find the index of multiple items in an Array. However, I found that it only returned the index of the first item that matched my condition or callback function.
For example, let's say we have an Array with ages and we want the indexes of all ages greater than or equal to 18.
let ages = [12,15, 18, 17, 21];
console.log(`Over 18: ${ages.findIndex(item => item >= 18)}`);
// expected output: [2,4]
// actual output: 2
So I'm curious if the Array.prototype.findIndex()
method only returns the index of the first item that matches, or -1
if no item satisfies the condition. Is there a way to achieve this using ES6?
Thank you