如何在JavaScript中向数组添加新的键和值?

时间:2019-03-25 07:40:50

标签: javascript arrays filter

我有两个数组,假设具有下面的数组值:“

var array1 = [
{Id: "809cd136-02c7-4cc8-b9de-04fd3359b265", Name: "testing"},
{Id: "609d3a78-8f7c-4843-acdb-2dcfc73c0d96", Name: "Delhi"},
{Id: "264d54cb-b104-48ed-91db-673327ae8d0e", Name: "rohit-auditor"},
{Id: "ce9691b3-dc55-4d30-baf4-7987c2b49b3e", Name: "test"},
{Id: "284e9e98-8ed7-4fb7-b09f-5d1f2a668b15", Name: "aman"}
] 

第二个数组是:

var array2 = ["809cd136-02c7-4cc8-b9de-04fd3359b265", "609d3a78-8f7c-4843-acdb-2dcfc73c0d96"]

现在,我只想在值等于数组一的那些对象中在array1中添加一个新的键值。换句话说,要同时匹配两个数组,并希望在具有相等值的数组中添加“ status = true”。

要添加的新密钥是:

{status: true}

现在我的新数组应该是:

[
{Id: "809cd136-02c7-4cc8-b9de-04fd3359b265", Name: "testing", status: true},
{Id: "609d3a78-8f7c-4843-acdb-2dcfc73c0d96", Name: "Delhi", status: true},
{Id: "264d54cb-b104-48ed-91db-673327ae8d0e", Name: "rohit-auditor"},
{Id: "ce9691b3-dc55-4d30-baf4-7987c2b49b3e", Name: "test"},
{Id: "284e9e98-8ed7-4fb7-b09f-5d1f2a668b15", Name: "aman"}

]

希望您能理解

预先感谢

4 个答案:

答案 0 :(得分:3)

您可以像这样使用forEachfind

let array1=[{Id:"809cd136-02c7-4cc8-b9de-04fd3359b265",Name:"testing"},{Id:"609d3a78-8f7c-4843-acdb-2dcfc73c0d96",Name:"Delhi"},{Id:"264d54cb-b104-48ed-91db-673327ae8d0e",Name:"rohit-auditor"},{Id:"ce9691b3-dc55-4d30-baf4-7987c2b49b3e",Name:"test"},{Id:"284e9e98-8ed7-4fb7-b09f-5d1f2a668b15",Name:"aman"}],
    array2=["809cd136-02c7-4cc8-b9de-04fd3359b265","609d3a78-8f7c-4843-acdb-2dcfc73c0d96"]
    
array2.forEach(id => {
  let found = array1.find(a => a.Id === id);
  if(found)
    found.status = true
})

console.log(array1)

此处进行if检查,以检查Id中是否存在array2中的array1。如果Id中的每个array2array1中都存在,则可以将其更改为:

array1.find(a => a.Id === id).status = true

答案 1 :(得分:1)

您可以使用Array#map方法来迭代并创建一个新数组,然后使用Array#includes方法来检查array2中存在的值。使用ES6 spread syntax组合两个对象。

var newArray = array1.map(o => array2.includes(o.Id) ? {...o,  ...add} : { ...add })

var array1 = [{
    Id: "809cd136-02c7-4cc8-b9de-04fd3359b265",
    Name: "testing"
  },
  {
    Id: "609d3a78-8f7c-4843-acdb-2dcfc`Array#forEach`73c0d96",
    Name: "Delhi"
  },
  {
    Id: "264d54cb-b104-48ed-91db-673327ae8d0e",
    Name: "rohit-auditor"
  },
  {
    Id: "ce9691b3-dc55-4d30-baf4-7987c2b49b3e",
    Name: "test"
  },
  {
    Id: "284e9e98-8ed7-4fb7-b09f-5d1f2a668b15",
    Name: "aman"
  }
]
var array2 = ["809cd136-02c7-4cc8-b9de-04fd3359b265", "609d3a78-8f7c-4843-acdb-2dcfc73c0d96"]

var add = {
  status: true
};

var newArray = array1.map(o => array2.includes(o.Id) ? {...o,  ...add} : { ...add })

console.log(newArray);


如果要更新原始数组,则只需使用Array#forEach方法对数组进行迭代,并在必要时使用Object.assign添加其他属性。

array1.forEach(o => array2.includes(o.Id) && Object.assign(o,add))

var array1 = [{
    Id: "809cd136-02c7-4cc8-b9de-04fd3359b265",
    Name: "testing"
  },
  {
    Id: "609d3a78-8f7c-4843-acdb-2dcfc73c0d96",
    Name: "Delhi"
  },
  {
    Id: "264d54cb-b104-48ed-91db-673327ae8d0e",
    Name: "rohit-auditor"
  },
  {
    Id: "ce9691b3-dc55-4d30-baf4-7987c2b49b3e",
    Name: "test"
  },
  {
    Id: "284e9e98-8ed7-4fb7-b09f-5d1f2a668b15",
    Name: "aman"
  }
]
var array2 = ["809cd136-02c7-4cc8-b9de-04fd3359b265", "609d3a78-8f7c-4843-acdb-2dcfc73c0d96"]

var add = {
  status: true
};

array1.forEach(o => array2.includes(o.Id) && Object.assign(o, add))

console.log(array1);

答案 2 :(得分:0)

简单的mapindexOf就足够了

var array1 = [
    {Id: "809cd136-02c7-4cc8-b9de-04fd3359b265", Name: "testing"},
    {Id: "609d3a78-8f7c-4843-acdb-2dcfc73c0d96", Name: "Delhi"},
    {Id: "264d54cb-b104-48ed-91db-673327ae8d0e", Name: "rohit-auditor"},
    {Id: "ce9691b3-dc55-4d30-baf4-7987c2b49b3e", Name: "test"},
    {Id: "284e9e98-8ed7-4fb7-b09f-5d1f2a668b15", Name: "aman"}
    ] 

    var array2 = ["809cd136-02c7-4cc8-b9de-04fd3359b265", "609d3a78-8f7c-4843-acdb-2dcfc73c0d96"]


    var result=array1.map(el=>{

        if(array2.indexOf(el.Id)>-1){
        el.status=true
      }
      return el
    })

答案 3 :(得分:0)

您必须将second array的元素与id's of element of first array进行比较,并将密钥(status)添加到找到的匹配项中。

var array1 = [{Id: "809cd136-02c7-4cc8-b9de-04fd3359b265",Name: "testing"},{Id: "609d3a78-8f7c-4843-acdb-2dcfc73c0d96", Name: "Delhi"},{Id: "264d54cb-b104-48ed-91db-673327ae8d0e", Name: "rohit-auditor"},{Id: "ce9691b3-dc55-4d30-baf4-7987c2b49b3e", Name: "test"},{Id: "284e9e98-8ed7-4fb7-b09f-5d1f2a668b15", Name: "aman"}]; 
var array2 = ["809cd136-02c7-4cc8-b9de-04fd3359b265", "609d3a78-8f7c-4843-acdb-2dcfc73c0d96"];

array2.forEach(function(secondElem) {
   let matchingElemInFirstArr = array1.find(el=>el.Id==secondElem);
   matchingElemInFirstArr ['status']=true;
 });

console.log(array1);