Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Created September 16, 2019 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/378e7bc0047a0841752d043a5e937c4d to your computer and use it in GitHub Desktop.
Save kellenmace/378e7bc0047a0841752d043a5e937c4d to your computer and use it in GitHub Desktop.
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