0

Given an array like this:

[{a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}]

Is there any (easy) way to return the an array of the same objects, but just with a selected property? E.g. let's say I only wanted to retain property "a" ...

[{a: 1}, {a: 3} {a: 5}] 
1
  • let result = arr.map(({a}) => ({a})) Commented Nov 30, 2018 at 23:10

1 Answer 1

2

Possible solution (little bit more generic than Ibrahim's comment):

const a = [{a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}];

const f = (arr, prop) => a.map(({ [prop]: propToKeep }) => ({ [prop]: propToKeep }));

console.log(f(a, 'a'));

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.