将对象数组与数组进行比较



我有一个对象数组:

arr1 = [
{
"id": 1,
"color": "blue",
"label": "School",
},
{
"id": 2,
"color": "red",
"label": "Work",
}
]

和一个简单的阵列:

arr2 = [ 2, 5 ]

我想写一个方法,如果在arr2中可以找到arr1中的一个对象id,则返回true。所以我可以用

v-if

稍后。你有什么建议?

let arr1 = [
{
"id": 1,
"color": "blue",
"label": "School",
},
{
"id": 2,
"color": "red",
"label": "Work",
}
]
let arr2 = [ 2, 5 ]
let arr1_id = arr1.map(function (obj) {
return obj.id;
});
function check(array1, array2) {
let intersection = array1.filter(element => array2.includes(element));
if (intersection.length > 0) {
return true
}
else {return false}
}
bool = check(arr1_id,arr2)
console.log(bool)

相关内容

  • 没有找到相关文章

最新更新