<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
let arr1 = [{
id: 1,
name: "甲"
},
{
id: 2,
name: "乙"
},
{
id: 3,
name: "丙"
}
]
let arr2 = [{
id: 2,
name: "B"
},
{
id: 3,
name: "C"
}
]
//数组过滤
let arr3 = arr1.filter(obj => arr2.some(obj1 => obj1.id == obj.id))
// console.info(arr3); //输出:[{id: 2,name: "乙"},{id: 3,name: "丙"}]
//两层遍历,进行匹配
let newArr = [];
arr1.forEach(item => {
arr2.forEach(i => {
if (i.id == item.id) {
i.sex = '1'
newArr.push(i);
}
})
})
console.log(newArr); //输出:[{id: 2,name: "乙"},{id: 3,name: "丙"}]
</script>
</body>
</html>