Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add element and remove element from array through javascript

let objArr = [
  { "name" : "mark", "height" : "tall", "hairColor": "black"},
  { "name" : "ben", "height" : "medium", "color": "fair"},
  { "name" : "neil", "height" : "small", "color": "dark"}
];

addmoreObject = {"gender": "male", "age": 33};

const res = objArr.map(({ hairColor, color, addmoreObject ...r }) => r);
console.log("RES", res)

I wanted to remove color and hairColor from object and wanted to add more element like gender, age etc. how can I make it in correct manner please guide

https://jsfiddle.net/v0yLu8m2/

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You’re very close:

// Change
const res = objArr.map(({ hairColor, color, addmoreObject ...r }) => r);

// To
const res = objArr.map(({ hairColor, color, ...rest }) => {
  return {
    ...rest,
    ...addmoreObject,
  };
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading