// 两个示例数组
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,并在匹配的对象中添加一个状态字段
猜你喜欢
转载自blog.csdn.net/qq_40745143/article/details/132672335
今日推荐
周排行