Javascript Jquery,更新数组中的JSON对象
嗨,我在为JSON对象更新临时数组中的对象时遇到了困难 主要目标是:Javascript Jquery,更新数组中的JSON对象,javascript,jquery,arrays,json,object,Javascript,Jquery,Arrays,Json,Object,嗨,我在为JSON对象更新临时数组中的对象时遇到了困难 主要目标是: 添加不是上一个的对象。已在地图标记数组中 如果存在具有相同ID的对象,则更新位置 如果JSON没有标记,则从map_标记数组中移除标记 这是我的JSON输出,它的变化很大程度上取决于用户输入 { "markers": [{ "ID": "1", "Area": "SomeArea1", "Status": "on", "Latitude": 58.9667
{
"markers": [{
"ID": "1",
"Area": "SomeArea1",
"Status": "on",
"Latitude": 58.96674,
"Longitude": 16.204277,
"Class": "A",
"Time": "2016-03-12 00:21"
}, {
"ID": "3",
"Area": "SomeArea",
"Status": "off",
"Latitude": 59.000351,
"Longitude": 16.227819,
"Class": "A",
"Time": "2016-03-12 00:22"
}]
}
JS
任何帮助和可能的小提琴将是有益的
谢谢
var loadMarkers = function () {
$.getJSON('map/map_json.php', function (data) {
$.each(data.markers, function (i, index) {
console.log("i =" + i + " Index =" + index.ID);
if (map_Markers[i].ID === index.ID) {
console.log("Updating marker position!");
map_Markers[i].setPosition(new google.maps.LatLng(index.Latitude, index.Longitude));
} else if (map_Markers[i].ID != index.ID) {
console.log("Remove Marker!");
map_Markers.splice(i, 1);
} else {
console.log("Creating new Marker!");
var myLatlng = new google.maps.LatLng(index.Latitude, index.Longitude);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: index.Area,
id: index.ID
});
console.log(index.ID);
map_Markers.push(marker);
} //If
}); //each loop
console.log("map_Marker Array : " + map_Markers.length + " Object");
console.log(map_Markers);
}); //getJSON
}