Combine objects and delete a property with JavaScript

JavascriptWeb DevelopmentObject Oriented Programming

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' }
raja
Published on 18-Aug-2020 10:47:15
Advertisements