-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'}
]
3
  • 1
    Delete unnecessary properties, create new objects with only the relevant properties, ... Commented Apr 20, 2018 at 15:24
  • Exact Duplicate: stackoverflow.com/questions/48396052/… Commented Apr 20, 2018 at 15:42
  • unlucky use of "reduce" in question title Commented Jul 14, 2023 at 0:40

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.