如何通过JavaScript向数组添加元素和移除元素

huangapple go评论73阅读模式
英文:

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, ...r }) => r);
console.log("RES", res);
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

addmoreObject = {&quot;gender&quot;: &quot;male&quot;, &quot;age&quot;: 33};

const res = objArr.map(({ hairColor, color, addmoreObject ...r }) =&gt; r);
console.log(&quot;RES&quot;, res)

<!-- end snippet -->

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/

答案1

得分: 2

你很接近:

// 更改为
const res = objArr.map(({ hairColor, color, ...rest }) => {
  return {
    ...rest,
    ...addmoreObject,
  };
});
英文:

You're very close:

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

// To
const res = objArr.map(({ hairColor, color, ...rest }) =&gt; {
  return {
    ...rest,
    ...addmoreObject,
  };
});

答案2

得分: 0

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

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

    let newArr = objArr.map(({ hairColor, color, ...rest }) => ({ ...rest, ...addmoreObject }));
    console.log(newArr);
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

let addmoreObject = {&quot;gender&quot;: &quot;male&quot;, &quot;age&quot;: 33};

let newArr = objArr.map(({ hairColor, color, ...rest }) =&gt; ({ ...rest, ...addmoreObject }));
console.log(newArr);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 22:33:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定