-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
  • 1,031,962
  • 187
  • 1,923
  • 1,875
new_user503
  • 51
  • 13

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
  • 638
  • 9
  • 26