I have an array of car objects:
[
{
id: 1,
make: "Honda",
model: "Civic",
style: "Coupe"
},
{
id: 2,
make: "Toyota",
model: "Corolla",
style: "Sedan"
},
{
id: 3,
make: "Subaru",
model: "Forester",
style: "Hatchback"
},
]
Ex: I can use the bracket notation to search by the key
cars.map(car => car["make"])
// Returns an array of the make ["Honda", "Toyota", "Subaru"], as expected
Which leads me to believe, if I try to make a generic function that can pass the key as a property, which in theory should return the same thing.
Typo updated (It was correct on my application):
function DisplayItems(desiredKey) {
return cars.map(car => car[desiredKey])
}
// Call it at a later point
DisplayItems("make")
// This is returning undefined
Why is this returning undefined?