0

Let's say we have an array of object:

var obj = [{a: 1, b: 2, c: 3, d: 4, e: 5 },{a: 6, b: 7, c: 
8, d: 9, e: 0 }];

and we want to delete key c,e from both of the objects.

How can it be done? One of the methods I found is:

['c', 'e'].forEach(e => delete obj[e]); //for object

Is there any other way so we don't have to use double for loop.

2
  • 3
    obj.map(({ c, e, ...rest }) => rest) would create a new array of new objects with those keys removed. Commented Jan 12, 2021 at 9:55
  • Is there a specific reason why you don't want to use a double for loop? There should be very little difference for performance and it's the only option which is actually dynamic (in case ['c', 'e'] is actually not known at build, only runtime). Commented Feb 16, 2023 at 19:41

2 Answers 2

4

One way to do it is to use .map() together with object destructuring:

var obj = [
  { a: 1, b: 2, c: 3, d: 4, e: 5 },
  { a: 6, b: 7, c: 8, d: 9, e: 0 },
];

var newObj = obj.map(({ c, e, ...rest }) => rest);
console.log(newObj)

This will create a new array with new objects which contain all of the other keys except for c and e.

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

4 Comments

Thank you so much....but could you please tell me why we used "...rest" for?
@SangamBasnet ({ c, e, ...rest }) will get c and e and store them in separate variables and all other properties will be stored in new object named rest. This is what the above syntax means
When you destructure an object (e.g. const {c, e} = {a:1, c: 2, e: 3}), you can choose the variables you want to pick out of the object. The ... in there is called "rest" and all other keys which were not explicitly picked out are stored in that object. The name of the variable can be anything, it doesn't have to be "rest".
The names are pretty intuitive actually: ...rest simply collects all the "rest", i. e. the remaining properties of the destructured object - after c and e had been removed and places them together again in an object called rest.
2

You have 2 options to resolve it:

  1. By using object destructuring: map(({ a,b,c,d,e }) => ({a,b,d})
  2. Enhance option 1 by using using [Rest parameters] { c, e, ...rest }
  • Object destructuring like below

    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 } var {c, e} = obj; // c = 3, e = 5

  • With option 2, you will have c,e implicit name and the remaining items named rest. After that, you just need to get rest items.

Option 1

var obj = 
[
  { a: 1, b: 2, c: 3, d: 4, e: 5 },
  { a: 6, b: 7, c: 8, d: 9, e: 0 },
];
console.log(obj.map(({ a,b,c,d,e }) => ({a,b,d})));

Option 2

var obj = 
[
  { a: 1, b: 2, c: 3, d: 4, e: 5 },
  { a: 6, b: 7, c: 8, d: 9, e: 0 },
];
         
console.log(obj.map(({ c, e, ...rest }) => rest));
                           // ...rest: the same as `a,b,d`

3 Comments

There is an existing answer above which is same as this ...
Oh, I have more explanation with the same idea. Thanks @Codenewbie
@Sangam Basnet Does this answer your question? Let me know if you need any help.

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.