大致你可以做这样的事情,
更新的工作代码
var newIDs; // new IDs found from ajax
newIDs = [1,2]
markersArray = [{ID:1},{ID:8},{ID:2},{ID:4},{ID:6}]
var newMarkers = $.map(markersArray,function(obj,i){
if(($.inArray(obj.ID,newIDs) == -1))
return null;
else
return obj;
});
console.log(newMarkers);
// now newMarkers holds the filtered markers Array
修改上面的代码以满足您的需要。
$.map $.inArray
检查工作小提琴
找出从服务器发送的额外 ID 的最终代码也是:小提琴
var newIDs; // new IDs found from ajax
newIDs = [1,2,3,4,5];
markersArray = [{ID:1},{ID:8},{ID:2},{ID:4},{ID:6}];
console.log('Orig Array: ',markersArray);
console.log('From Server: ',newIDs);
var newMarkers = $.map(markersArray,function(obj,i){
return $.inArray(obj.ID,newIDs) == -1? null: obj;
});
console.log('New Markers Objects: ',newMarkers);
var newMarkerIDs = $.map(markersArray,function(obj,i){
return $.inArray(obj.ID,newIDs) == -1? null:obj.ID;
});
console.log('New Markers ID Array: ',newMarkerIDs);
var extraIDs = $.map(newIDs,function(val,i){
return $.inArray(val,newMarkerIDs) == -1 ? val : null;
});
console.log('Extra IDs: ',extraIDs);