-2

I have the following array of objects:

  names: [
    {
      "first": "Bob",
      "last": "Newhart"
    },
    {
      "first": "Jerry",
      "last": "Seinfeld"
    },
    {
      "first": "Oprah",
      "last": "Winfrey"
    }
  ]

I want to convert this into a simple array that looks like this:

names: [ "Bob Newhart", "Jerry Seinfeld", "Oprah Winfrey"]

What is the proper way to do this in JavaScript?

dwax
  • 381
  • 7
  • 19

1 Answers1

0

Map each value and concatenate first and last name

names.map(obj => obj.first + ' ' + obj.last)

var names = [{"first": "Bob","last": "Newhart"},{"first": "Jerry","last": "Seinfeld"},{"first": "Oprah","last": "Winfrey"}]
  
names = names.map(obj => obj.first + ' ' + obj.last)
  
console.log(names)