How can I access all values with the same key in an object array? Given the following data:
data = [{first_name: 'Peter', last_name: 'Smith', age: 45},
{first_name: 'John', last_name: 'Miller', age: 21}];
Is there a easy way to get an array containing all first names, like first_names = ['Peter', 'John']
?
I guess I'm asking a very frequently asked question, but I couldn't find a solution anywhere to answer it.
The map() method creates a new array with the results of calling a provided function on every element in the calling array.
data = [{
first_name: 'Peter',
last_name: 'Smith',
age: 45
},
{
first_name: 'John',
last_name: 'Miller',
age: 21
}
];
var first_names = data.map(ele => ele.first_name);
console.log(first_names)