我想删除这个json对象中有{"lat":0,"lng":0}
的项目,这里是对象:
[{
"lat": 35.77118697154732,
"lng": -5.809084439749561
}, {
"lat": 35.77118697154732,
"lng": -5.809084439749562
}, {
"lat": 0,
"lng": 0
}, {
"lat": 0,
"lng": 0
}, {
"lat": 0,
"lng": 0
}]
我试过了:
storyboard.deleteLngAndLatEqualZeo = function(data) {
for (var i = 0; i < data.length; i++) {
var currentData = data[i];
if (currentData.lat == 0 && currentData.lng == 0) {
data.splice(i, 1);
}
}
console.log(JSON.stringify(data));
}
它不起作用。有人可以帮忙吗?
答案 0 :(得分:2)
您可以使用slice
,结帐我的回答
for (var i = data.length - 1; i > -1; i--) {
var latData=data[i].lat;
var lngData=data[i].lng;
if (put condition) {
data.splice(i, 1);
}
}
答案 1 :(得分:1)
您可以使用Array.prototype.filter()这样的功能:
var data = [{
"lat": 35.77118697154732,
"lng": -5.809084439749561
}, {
"lat": 35.77118697154732,
"lng": -5.809084439749562
}, {
"lat": 0,
"lng": 0
}, {
"lat": 0,
"lng": 0
}, {
"lat": 0,
"lng": 0
}];
var result = data.filter(function(d) {
return !(d.lat === 0 && d.lng === 0);
});
console.log(result);
答案 2 :(得分:0)
data =
[{
"lat": 35.77118697154732,
"lng": -5.809084439749561
}, {
"lat": 35.77118697154732,
"lng": -5.809084439749562
}, {
"lat": 0,
"lng": 0
}, {
"lat": 0,
"lng": 0
}, {
"lat": 0,
"lng": 0
}];
deleteLngAndLatEqualZeo = function(data) {
for (var i = 0; i < data.length; i++) {
var currentData = data[i];
if (currentData.lat == 0 && currentData.lng == 0) {
data.splice(i, 1);
--i;
}
}
console.log(JSON.stringify(data));
}
deleteLngAndLatEqualZeo(data);