我很抱歉,如果以前已经解决了这个问题,但是我无法使它与我找到的任何内容一起使用。
假设我有2个数组-arr1
,arr2
。如果属性arr1
在id
和arr1
中匹配,我想更新arr2
中的对象。 arr2
中存在但arr1
中不存在的对象-表示属性id
在arr1
中不存在-应该被推到arr1
。
示例:
let arr1 = [
{id: 0, name: "John"},
{id: 1, name: "Sara"},
{id: 2, name: "Domnic"},
{id: 3, name: "Bravo"}
]
let arr2 = [
{id: 0, name: "Mark"},
{id: 4, name: "Sara"}
]
# Expected Outcome
let outcome = [
{id: 0, name: "Mark"},
{id: 1, name: "Sara"},
{id: 2, name: "Domnic"},
{id: 3, name: "Bravo"},
{id: 4, name: "Sara"}
]
答案 0 :(得分:1)
const arr1 = [
{id: 0, name: "John"},
{id: 1, name: "Sara"},
{id: 2, name: "Domnic"},
{id: 3, name: "Bravo"}
];
const arr2 = [
{id: 0, name: "Mark"},
{id: 4, name: "Sara"}
];
arr2.reduce((res, item) => {
const existingItem = res.find(x => x.id === item.id);
if (existingItem) { existingItem.name = item.name; }
else { res.push(item); }
return res;
}, arr1);
console.log(arr1);
答案 1 :(得分:1)
您可以这样做:
$END$
答案 2 :(得分:0)
假设您要对If ThisWorkbook.Worksheets("Annexure").Range("G8").Value = 2 Then
bot.FindElementByXPath("//a[@class='add btn-add-raw-materials action']//i[@class='fa fa-plus-circle']").Click
ElseIf ThisWorkbook.Worksheets("Annexure").Range("G8").Value = 3 Then
bot.FindElementByXPath("//a[@class='add btn-add-raw-materials action']//i[@class='fa fa-plus-circle']").Click
bot.FindElementByXPath("//a[@class='add btn-add-raw-materials action']//i[@class='fa fa-plus-circle']").Click
ElseIf ThisWorkbook.Worksheets("Annexure").Range("G8").Value = 4 Then
bot.FindElementByXPath("//a[@class='add btn-add-raw-materials action']//i[@class='fa fa-plus-circle']").Click
bot.FindElementByXPath("//a[@class='add btn-add-raw-materials action']//i[@class='fa fa-plus-circle']").Click
bot.FindElementByXPath("//a[@class='add btn-add-raw-materials action']//i[@class='fa fa-plus-circle']").Click
ElseIf ThisWorkbook.Worksheets("Annexure").Range("G8").Value = 5 Then
bot.FindElementByXPath("//a[@class='add btn-add-raw-materials action']//i[@class='fa fa-plus-circle']").Click
bot.FindElementByXPath("//a[@class='add btn-add-raw-materials action']//i[@class='fa fa-plus-circle']").Click
bot.FindElementByXPath("//a[@class='add btn-add-raw-materials action']//i[@class='fa fa-plus-circle']").Click
bot.FindElementByXPath("//a[@class='add btn-add-raw-materials action']//i[@class='fa fa-plus-circle']").Click
End If
bot.FindElementById("export-HS-code").SendKeys ThisWorkbook.Worksheets("Annexure").Range("B11")
bot.FindElementById("raw-material-description-1").SendKeys ThisWorkbook.Worksheets("Annexure").Range("C11")
bot.FindElementById("raw-material-quantity-unit-1").SendKeys ThisWorkbook.Worksheets("Annexure").Range("D11")
bot.FindElementByName("raw_material_unit[1]").SendKeys ThisWorkbook.Worksheets("Annexure").Range("E11")
bot.FindElementById("raw-material-unit-cif-value-1").SendKeys ThisWorkbook.Worksheets("Annexure").Range("F11")
bot.FindElementById("raw-material-total-cif-value-1").SendKeys ThisWorkbook.Worksheets("Annexure").Range("G11")
bot.FindElementByName("raw_country_id[1]").SendKeys ThisWorkbook.Worksheets("Annexure").Range("H11")
bot.FindElementById("bill-invoice-no-1").SendKeys ThisWorkbook.Worksheets("Annexure").Range("I11")
bot.FindElementById("bill-invoice-date-1").SendKeys ThisWorkbook.Worksheets("Annexure").Range("J11")
bot.FindElementById("supplier-details-1").SendKeys ThisWorkbook.Worksheets("Annexure").Range("K11")
中的对象进行突变而不是创建新对象,一种方法是使用for...of
对arr1
中的对象进行迭代,然后检查是否存在已使用Array.prototype.find()
在arr2
中具有相同id
的对象:
Object.assign
对其进行突变。arr1
:
arr1
const arr1 = [
{ id: 0, name: 'John' },
{ id: 1, name: 'Sara' },
{ id: 2, name: 'Domnic' },
{ id: 3, name: 'Bravo' },
];
const arr2 = [
{ id: 0, name: 'Mark', sometingElse: 123 },
{ id: 2, foo: 'bar' },
{ id: 4, name: 'Sara' },
];
for (const currentElement of arr2) {
let previousElement = arr1.find(el => el.id === currentElement.id);
if (previousElement) {
Object.assign(previousElement, currentElement);
} else {
arr1.push(currentElement);
}
}
console.log(arr1);
答案 3 :(得分:0)
如果您想尝试其他方法,可以使用foreach和filter来实现
let arr1 = [
{id: 0, name: "John"},
{id: 1, name: "Sara"},
{id: 2, name: "Domnic"},
{id: 3, name: "Bravo"}
]
let arr2 = [
{id: 0, name: "Mark"},
{id: 4, name: "Sara"}]
arr1.forEach(x=>{
arr2.forEach(y=>{
if(x.id==y.id){
x.name=y.name
}
})
})
arr2.filter((a)=>{if(!arr1.some(b=>a.id==b.id)) arr1.push(a)})
console.log(arr1)
答案 4 :(得分:-1)
您应该可以使用Array.prototype.find
来解决这个问题!
let arr1 = [
{id: 0, name: "John"},
{id: 1, name: "Sara"},
{id: 2, name: "Domnic"},
{id: 3, name: "Bravo"}
];
let arr2 = [
{id: 0, name: "Mark"},
{id: 4, name: "Sara"}
];
let updateArrayOfObjects = (arr1, arr2) => {
for (let obj of arr2) {
let item = arr1.find(v => v.id === obj.id);
if (item) item.name = obj.name;
else arr1.push({ ...obj });
}
return arr1;
};
console.log(updateArrayOfObjects(arr1, arr2));