Combine objects and delete a property with JavaScript


We have the following array of objects that contains two objects and we are required to combine both objects into one and get rid of the chk property altogether −

const err = [
   {
      "chk" : true,
      "name": "test"
   },
   {
      "chk" :true,
      "post": "test"
   }
];

Step 1 − Combining objects to form a single object

const errObj = Object.assign(...err);

Step 2 − Removing the chk property

delete errObj['chk'];
console.log(errObj);

Let us now see the entire code with output −

Example

const err = [
   {
      "chk" : true,
      "name": "test"
   },
   {
      "chk" :true,
      "post": "test"
   }
];
const errObj = Object.assign(...err);
delete errObj['chk'];
console.log(errObj);

Output

Output in the console will be −

{ name: 'test', post: 'test' }

Updated on: 18-Aug-2020

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements