我有两个数组,让我们假设有数组的值:“
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"}
]
希望你能理解。
提前致谢,
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
您可以使用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
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);
一个简单的map
和indexOf
就足够了
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
})
你必须将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);