If I have an array of objects, for example:
var cars = [{name:"Geronimo", color:"red"},{name:"Ronaldo",color:"green"}]
Is there a simple way Im not seeing to select all the "color" attributes? So that I would get:
"red", "green"
So say something like (INVENTED):
console.log(cars[selectingallobjects].name)
Thanks in advance!
>Solution :
There isn’t a ready way of doing that, you need to iterate to get the values.
You can use .map(), an Array method that creates a new array based on the values returned from the function.
It can be done in a single line.
See below:
var cars = [{name:"Geronimo", color:"red"},{name:"Ronaldo",color:"green"}]
let result = cars.map(car => car.color)
console.log(result)