0

I am sure what I am trying to accomplish can be done in a much simpler way. I unpack multiple fields and then I add the filtered object to an array.

const filteredPersons = [];
const {state, name, age, weight, gpa, diet} = person;
filteredPersons.push({state, name, age, weight, gpa, diet});

When I try something like this, it does not work:

const filteredPerson = {state, name, age, weight, gpa, diet} = person;
filteredPersons.push(filteredPerson);

This causes repetition and it looks really ugly.

  • Depends on what `person` is. You might want to `push(person)` or `push({...person})`? But if you want to take individual properties, it won't really get any simpler. – Bergi May 05 '22 at 21:59

1 Answers1

0

If there just couple of props you dont need to include from person means, you can use the following.

const filteredPersons = [];
const { unwanted1, unwanted2, ...restProps } = person;
filteredPersons.push(restProps);
Siva K V
  • 10,561
  • 2
  • 16
  • 29