Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript Array Filter to get Indices
const cars = [
{make: "ford", model: "mustang"},
{make: "toyota", model: "camry"},
{make: "ford", model: "fiesta"},
{make: "chevrolet", model: "volt"},
{make: "ford", model: "escape"},
{make: "chrysler", model: "pacifica"},
]
const fordCarIndices = cars.reduce((fordCarIndices, field, index) => {
if (field.make === "ford") {
fordCarIndices.push(index)
}
return fordCarIndices
}, [])
console.log(fordCarIndices)
// Output: [0, 2, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.