-3

how can I reduce an array of objects of this type:

[
 {id: 1, name: 'jhon', lastname: 'doe', age: 30},
 {id: 2, name: 'max', lastname: 'wall', age: 20},
 {id: 3, name: 'fer', lastname: 'baneg', age: 15}
]

I need to get a new array just with these values:

[
 {id: 1, name: 'jhon'},
 {id: 2, name: 'max'},
 {id: 3, name: 'fer'}
]
FeRcHo
  • 969
  • 5
  • 11
  • 26
  • 1
    Delete unnecessary properties, create new objects with only the relevant properties, ... – Andreas Apr 20 '18 at 15:24
  • Exact Duplicate: https://stackoverflow.com/questions/48396052/how-to-remove-keyvalue-pair-from-an-object-of-array – Kevin B Apr 20 '18 at 15:42

1 Answers1

3

You can use map() method for this with ES6 parameter destructuring.

const data = [{id: 1, name: 'jhon', lastname: 'doe', age: 30},{id: 2, name: 'max', lastname: 'wall', age: 20},{id: 3, name: 'fer', lastname: 'baneg', age: 15}]

const result = data.map(({id, name}) => ({id, name}))
console.log(result)
Nenad Vracar
  • 111,264
  • 15
  • 131
  • 153