var tagIDArr =[{name: "博客a", id: 7}, {name: "博客b", id: 8}, {name: "博客c", id: 9}];
var tagList =[{name: "博客1", index: 7}, {name: "博客2", index: 5}, {name: "博客3", index: 9}];
tagIDArr.forEach(function(arr){
tagList.forEach(function(list){
if(arr.id == list.index){
// console.log(arr); //输入 {name: "博客a", id: 7} {name: "博客c", id: 9}
if (arr.id == 7) {
console.log(arr); //输入 {name: "博客a", id: 7}
}
}
});
});两个数组对象的交集forEach遍历
本文通过一个JavaScript示例展示了如何遍历两个数组并根据ID与索引匹配项。重点在于理解forEach循环及条件判断的应用,同时介绍了如何使用console.log进行调试。

被折叠的 条评论
为什么被折叠?



