我如何在 AngularJS 中比较两个数组并返回匹配的值?
这是第一个数组:
[{
"id":2,
"student_name":"LiSa",
"alien":"A",
"world":"Sun",
"justification":"i like sent this one",
"submit_time":null
},{
"id":1,
"student_name":"Liz",
"alien":"B",
"world":"Earth",
"justification":null,
"submit_time":"2014-09-25T08:37:34.526-05:00"
}]
这是第二个:
[{
"id":1,
"alien":"A",
"world":"Sun",
"color":"red"
},{
"id":2,
"alien":"A",
"world":"Mercury",
"color":"red"
},{
"id":3,
"alien":"B",
"world":"Earth",
"color":"red"
},{
"id":4,
"alien":"B",
"world":"Moon",
"color":"red"
}]
我想检查是否有以下值alien and world在这两个数组中是匹配的。然后我可以得到color来自第二个数组的值。
这是我在控制器中放入的代码:
angular.forEach(arr1, function(value1, key1) {
angular.forEach(arr2, function(value2, key2){
if(value1.alien === value2.alien && value1.world === value2.world){
console.log(value2.color);
}
});
});
我可以用吗angular.forEach
?我怎样才能做到这一点?我在哪里存储颜色值?