在下面的javascript对象中,我想检查数组中的每个问题id是否在数组1中找到
arrayOne=[{"question":"100","response":"aaaa"},
{"question":"200","response":"aaaa"}]
arrayTwo=[{"question":"100","output":true},
{"question":"200","output":true}]
示例
如果是arrayTwo
arrayTwo=[{"question":"100","output":true}]
返回false
如果是arrayTwo
arrayTwo=[{"question":"100","output":true},
{"question":"200","output":true}]
return true.
如何使函数检查两个数组?
答案 0 :(得分:1)
您可以使用every()
和find()
执行此操作,结果会返回true/false
。
var arrayOne = [{
"question": "100",
"response": "aaaa"
}, {
"question": "200",
"response": "aaaa"
}]
var arrayTwo = [{
"question": "100",
"output": true
}, {
"question": "200",
"output": true
}]
var result = arrayTwo.every(function(e) {
return arrayOne.find(function(a) {
return a.question == e.question;
})
})
console.log(result)