如何在 Jquery ajax 请求期间获取唯一字段
How to get unique fields during a Jquery ajax request
有谁知道如何从对象数组中过滤掉重复的数据吗?我有一个简单的父子列表。子列表有一个包含父 ID 的字段。
这是我的子数据的示例:
responseText: {
d: {
results: [{
ID_of_parent: "17",
Day_or_night: "day",
Start: "2016-06-01 08:00",
End: "2016-06-01 10:00",
Hours: "2"
}, {
ID_of_parent: "17",
Day_or_night: "day",
Start: "2016-06-01 13:00",
End: "2016-06-01 14:00",
Hours: "1"
}, {
ID_of_parent: "17",
Day_or_night: "night",
Start: "2016-06-01 21:00",
End: "2016-06-01 22:00",
Hours: "1"
}, {
ID_of_parent: "22",
Day_or_night: "day",
Start: "2016-06-01 09:00",
End: "2016-06-01 10:00",
Hours: "1"
}, {
ID_of_parent: "22",
Day_or_night: "day",
Start: "2016-06-01 14:00",
End: "2016-06-01 15:00",
Hours: "1"
}, {
ID_of_parent: "54",
Day_or_night: "day",
Start: "2016-06-01 13:30",
End: "2016-06-01 16:00",
Hours: "2.5"
}]
}
}
这是父数据:
responseText: {
d: {
results: [{
ID: "17",
description: "Description 1"
}, {
ID: "22",
description: "Description 2"
}, {
ID: "34",
description: "Description 3"
}, {
ID: "54",
description: "Description 4"
}]
}
}
我只是想完成的是通过 ajax 获取子列表数据,然后 foreach,删除重复的 ID,然后获取父 ID。目前这是我目前所拥有的:
$.ajax({
url:"Child_list_path",
dataType: "json",
cache: false,
success: function (child) {
$.each(child.d.results, function (index, children) {
$.ajax({
url: parent_list_path("+children.Incident_ID+")",//filter by ID
dataType : 'json',
cache : false,
success : function (parent) {
//stuff
}
});//end of inner ajax
});//end of each
}//sucess end
});
问题在于,在子数据中,可以有多个条目具有相同的父 ID。因此,在 for each 序列期间发生的事情是,当我只需要获取每个唯一 ID 的数据时,它会为同一 ID 多次提取过滤后的父数据。
编辑:
我的objective是为了能够过滤子数据中重复的ID_of_parent。因此,我不想通过 17、17、17、22、22、54 循环,而是希望它通过 17、22、54 循环。
感谢您的评论,我想通了。这就是我最终解决问题的方法。
$.ajax({
url:"Child_list_path",
dataType: "json",
cache: false,
success: function (child) {
child_temp_IDs = [];
$.each(child.d.results, function (index, children) {
if($.inArray(children.Incident_ID, child_temp_IDs) === -1) child_temp_IDs.push(children.Incident_ID);
});//end of each
$.each(child_temp_IDs , function (index, children) {
$.ajax({
url: parent_list_path("+children+")",
dataType : 'json',
cache : false,
success : function (parent) {
}
});
});
}
});
有谁知道如何从对象数组中过滤掉重复的数据吗?我有一个简单的父子列表。子列表有一个包含父 ID 的字段。
这是我的子数据的示例:
responseText: {
d: {
results: [{
ID_of_parent: "17",
Day_or_night: "day",
Start: "2016-06-01 08:00",
End: "2016-06-01 10:00",
Hours: "2"
}, {
ID_of_parent: "17",
Day_or_night: "day",
Start: "2016-06-01 13:00",
End: "2016-06-01 14:00",
Hours: "1"
}, {
ID_of_parent: "17",
Day_or_night: "night",
Start: "2016-06-01 21:00",
End: "2016-06-01 22:00",
Hours: "1"
}, {
ID_of_parent: "22",
Day_or_night: "day",
Start: "2016-06-01 09:00",
End: "2016-06-01 10:00",
Hours: "1"
}, {
ID_of_parent: "22",
Day_or_night: "day",
Start: "2016-06-01 14:00",
End: "2016-06-01 15:00",
Hours: "1"
}, {
ID_of_parent: "54",
Day_or_night: "day",
Start: "2016-06-01 13:30",
End: "2016-06-01 16:00",
Hours: "2.5"
}]
}
}
这是父数据:
responseText: {
d: {
results: [{
ID: "17",
description: "Description 1"
}, {
ID: "22",
description: "Description 2"
}, {
ID: "34",
description: "Description 3"
}, {
ID: "54",
description: "Description 4"
}]
}
}
我只是想完成的是通过 ajax 获取子列表数据,然后 foreach,删除重复的 ID,然后获取父 ID。目前这是我目前所拥有的:
$.ajax({
url:"Child_list_path",
dataType: "json",
cache: false,
success: function (child) {
$.each(child.d.results, function (index, children) {
$.ajax({
url: parent_list_path("+children.Incident_ID+")",//filter by ID
dataType : 'json',
cache : false,
success : function (parent) {
//stuff
}
});//end of inner ajax
});//end of each
}//sucess end
});
问题在于,在子数据中,可以有多个条目具有相同的父 ID。因此,在 for each 序列期间发生的事情是,当我只需要获取每个唯一 ID 的数据时,它会为同一 ID 多次提取过滤后的父数据。
编辑: 我的objective是为了能够过滤子数据中重复的ID_of_parent。因此,我不想通过 17、17、17、22、22、54 循环,而是希望它通过 17、22、54 循环。
感谢您的评论,我想通了。这就是我最终解决问题的方法。
$.ajax({
url:"Child_list_path",
dataType: "json",
cache: false,
success: function (child) {
child_temp_IDs = [];
$.each(child.d.results, function (index, children) {
if($.inArray(children.Incident_ID, child_temp_IDs) === -1) child_temp_IDs.push(children.Incident_ID);
});//end of each
$.each(child_temp_IDs , function (index, children) {
$.ajax({
url: parent_list_path("+children+")",
dataType : 'json',
cache : false,
success : function (parent) {
}
});
});
}
});