0

I have an array of objects:

[{
    name: "test",
    age: 20,
    gender: "male"
},
{
    name: "test2",
    age: 22,
    gender: "female"
}]

Frequently I need to create a singleton array which contains a specific property from the object array above, for example extract only the names from the array above and create an array from it:

NewArray = ["test","test2"]

Currently I loop over the object array and push the property I need to the new array.

Is there a quick way to do it in Javascript/ES instead of looping every time I need to get specific property?

Al.G.
  • 4,327
  • 6
  • 31
  • 56
TheUnreal
  • 23,434
  • 46
  • 157
  • 277

1 Answers1

0
var people = [{
  name:'test1',
  age:20
}, {
  name:'test2',
  age:30
}]
let names = people.map(function(item) {
  return item.name
});
console.log(names);