js请教:A数组的某个元素如何更新为B数组的值 ?
盘点前库存表
[
{'item':'aaa','model':'9×9×9','stock':'成品仓','qty':100},
{'item':'bbb','model':'5×5×5','stock':'成品仓','qty':200},
{'item':'ccc','model':'8×8×8','stock':'成品仓','qty':400}
]
盘点表(含有些空白废数据)
[
{'item':'aaa','qty':888},
{'item':'ccc','qty':999},
{'item':'','qty':},
{'item':'','qty':},
{'item':'','qty':}
]
希望得到:正确库存表
[
{'item':'aaa','model':'9×9×9','stock':'成品仓','qty':888},
{'item':'ccc','model':'8×8×8','stock':'成品仓','qty':999}
]
回复
1个回答
test
2024-07-02
function mergeArrays(arr1, arr2) {
let result = [];
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i].item === arr2[j].item && arr2[j].item !== '') {
result.push(Object.assign({}, arr1[i], arr2[j]));
}
}
}
return result;
}
let newArray = mergeArrays(arr1, arr2);
console.log(newArray);
回复
适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容