-2

I have an array of objects and I would like to merge all names into one array with a one liner.

[{
   id:1,
   name: 'peter'
},
{
  id:2,
  name: 'john'
}]

expected output:

['peter', 'john']
user16405471
  • 61
  • 1
  • 7

1 Answers1

0

You can map the array to create another array with the list of names:

const people = [{
   id:1,
   name: 'peter'
},
{
  id:2,
  name: 'john'
}];

const names = people.map(({ name }) => name);
Thomas Gak-Deluen
  • 2,759
  • 2
  • 28
  • 38
  • 1
    This is an obvious, often-repeated duplicate question. It's not useful to post answers to them. Please vote to close as duplicate instead. – T.J. Crowder Oct 24 '22 at 09:00