-2

I have an array of objects:

let tempArray = [
  {
    id: '1',
    name: 'Tom',
    age: 11
  },
  {
    id: '2',
    name: 'Jerry',
    age: 13
  }
  ...
]

How can I create a new array that would contain only name fields from all objects of the tempArray array?

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
new_user503
  • 41
  • 10

1 Answers1

2

Trying using map()

let tempArray = [
    {
        id: '1',
        name: 'Tom',
        age: 11
    },
    {
        id: '2',
        name: 'Jerry',
        age: 13
    }
]

const res = tempArray.map(i => i.name)
console.log(res)
Janie
  • 633
  • 9
  • 25