let arr1 = [
{ id: 1, name: "张三" },
{ id: 2, name: "李四" },
{ id: 3, name: "王五" }
];
let arr2 = [
{ id: 2, score: 80 },
{ id: 3, score: 90 },
{ id: 4, score: 85 }
];
let newArray = [];
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
if (arr1[i].id === arr2[j].id) {
newArray.push({ name: arr1[i].name, score: arr2[j].score });
break;
}
}
}
console.log(newArray); // 输出:[ { name: '李四', score: 80 }, { name: '王五', score: 90 } ]
js 俩个数组中的对象值进行遍历比较 其中某个相同的值一样 将另外一个值组成新的数组
最新推荐文章于 2023-07-29 00:34:11 发布