如何在javascript中为数组添加新的键和值?

问题描述 投票:1回答:4

我有两个数组,让我们假设有数组的值:“

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中添加一个新的键值,只在那些值等于数组1的对象中。换句话说,想要匹配两个数组,并希望在具有相同值的那些中添加“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"}

]

希望你能理解。

提前致谢,

javascript arrays filter
4个回答
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中的每个array2都存在于array1中,您只需将其更改为:

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

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);


If you want to update the original array then simply iterate over the array using Array#forEach method and add an additional property using Object.assign if necessary.
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);

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
    })

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);
© www.soinside.com 2019 - 2024. All rights reserved.