我有一个函数可以进行两次ajax调用。一次调用返回包含其他对象的Object;另一个是对象数组。
//Object 1
1: {
table_info: {
id: "1",
event_id: "JHDQ",
name: "test",
cap: "10",
seating: "OPEN",
updated: "2017-02-12 16:34:01",
position_x: "10",
position_y: "99",
count: 2
},
assignments: [{
participant_id: "99154143",
seat_number: "2"
},
{
participant_id: "99154149",
seat_number: "3"
}
]
},
3: {
table_info: {
id: "3",
event_id: "JHDQ",
name: "test",
cap: "10",
seating: "OPEN",
updated: "2017-02-12 16:17:00",
position_x: "10",
position_y: "99",
count: 2
},
assignments: [{
participant_id: "99154146",
seat_number: "2"
},
{
participant_id: "99155361",
seat_number: "3"
}
]
}
//Object 2
{
id: "99155361",
first_name: "gary",
last_name: "banker",
email: "test2",
company: null
}, {
id: "99155365",
first_name: "tim",
last_name: "banker",
email: "test1",
company: null
}
我需要遍历对象1才能到达赋值数组。对于赋值数组中的每个索引,我需要将participant_id与对象2中的每个ID进行比较。如果有匹配,请提醒("匹配"),否则,提醒("不匹配&# 34)。请让我知道我需要做些什么才能实现这个目标?
答案 0 :(得分:0)
//object with assignments
var objects = {};
//array of assignmenents
var objArray = [];
//foreach key on first object find assignements
Object.keys(objects).forEach(function(key) {
var assignments = objects[key].assignments || [];
//foreach assignment id match against id in the other array
assignments.forEach(function(assignment){
objArray.forEach(function(oAssignment){
if(assignment['participant_id'] === oAssignment.id){
alert('Found match for: ' + assignment['participant_id'] );
}
});
});
});