-2

I have some data (array with objects) that looks like the one below:

data = [
         {
           name: 'peter'
         },
         {
           name: 'paul'
         }
       ];

I need to convert the above data to this:

data = ['peter', 'paul'];

I've tried:

Object.values(data));

...but that didn't do the job.

How can I do this conversion?

  • As there's only one known property a simple `for` loop would be enough. Why did you choose `Object.values()`? And how did you use it? – Andreas Nov 19 '20 at 09:01

1 Answers1

2

a simple .map() would do

var desiredResult = data.map(element => element.name);

Docs about Array.prototype.map()

Pablo Recalde
  • 3,334
  • 1
  • 22
  • 47