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,175
  • 6
  • 34
  • 55
TheUnreal
  • 21,504
  • 41
  • 145
  • 251

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);
user2693928
  • 3,507
  • 2
  • 14
  • 25