Created
September 16, 2019 21:28
-
-
Save kellenmace/378e7bc0047a0841752d043a5e937c4d to your computer and use it in GitHub Desktop.
JavaScript Array Filter to get Indices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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