1

The following line works to form the new object from the original report object. Report object has the fields name, id, result plus bunch of other fields that I don't want in the new object.

let newReport = report.map( ({ name, id, result }) => ({ name, id, result }) );

However, I do not want to hard code the field names. Would like to get them from a variable.

I have a list of fields in the string array.

let fields = ['name', 'id', 'result']

I'd like to pass this string array to the map function to avoid hardcoding field names. The following line of code does not perform what I wanted. What would be an elegant way to grab just the fields I like? Thanks.

let newReport = report.map(({ ...fields }) => ({ ...fields }));
hc48
  • 11
  • 2

0 Answers0