3

This is my object:

"names": [{
        "first": "Linda"
    },
    {
        "first": "Maverick"
    },
];

How can I convert this, so that I get an array with just the first values:

["Linda", "Maverick"]
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44

3 Answers3

7

Simply use Array#map:

let data = {"names": [
  {"first": "Linda"},
  {"first": "Maverick"},
]};

let array = data.names.map(name => name.first);

console.log(array)
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
5

Just use map:

data.names.map(name => name.first)
hsz
  • 148,279
  • 62
  • 259
  • 315
4

Use map with destructuring:

var names = [
  {
    "first": "Linda"
  },
  {
    "first": "Maverick"
  },
];

var res = names.map(({
  first
}) => first);
console.log(res);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62