我有一個函數可以進行兩次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才能到達賦值數組。對於assignments數組中的每個索引,我需要將participant_id與對象2中的每個ID進行比較。如果匹配,則爲alert(「match」),否則爲alert(「no match」)。請讓我知道我爲了達到這個目標需要做什麼?
你可以包括'javascript',你已經試過在問題? – guest271314
請參閱http://stackoverflow.com/help/how-to-ask,http://stackoverflow.com/help/mcve – guest271314
您需要遍歷每個返回的值。你可以在object1上使用'forEach'。在'forEach'中,你需要遍歷任務列表,可能使用另一個'forEach'或者'for'。在該循環中,循環訪問object2並在if/else語句中使用'==='在當前賦值'participant_id'和當前object2'id'之間進行簡單的字符串比較。如果找到匹配的警報,則返回警報。 – haxxxton