// 两个示例数组
const array1 = [
{ id: 1, name: 'Object 1' },
{ id: 2, name: 'Object 2' },
{ id: 3, name: 'Object 3' },
];
const array2 = [
{ id: 2, description: 'Description 2' },
{ id: 4, description: 'Description 4' },
];
// 判断两个数组中的对象id是否相同,如果相同则添加状态字段
for (const obj1 of array1) {
for (const obj2 of array2) {
if (obj1.id === obj2.id) {
obj1.status = '匹配'; // 可以根据需要设置不同的状态值
}
}
}
// 输出结果
console.log(array1);
JS判断两个数组中的对象是否具有相同的id,并在匹配的对象中添加一个状态字段
最新推荐文章于 2024-05-23 15:57:22 发布