从文件(JSON)中删除如果包含值x
问题描述:
我有一个庞大的机场列表,我打算用于自动完成目的。从文件(JSON)中删除如果包含值x
我目前正试图摆脱一些对我无用的内容。这是文件的内容,怎么看起来像:
{ "Airports": [
{
"iata": "TSS",
"iso": "US",
"status": 1,
"name": "East 34th Street Heliport",
"continent": "NA",
"type": "heliport",
"size": null
},
{
"iata": "FOB",
"lon": "-123.79444",
"iso": "US",
"status": 1,
"name": "Fort Bragg Airport",
"continent": "NA",
"type": "airport",
"lat": "39.474445",
"size": "small"
},
我想删除一切那里type = heliport
最后保存回我airports.json
我怕我没有示例代码发布因为我不知道要走到这里的路是什么。
虽然我发现这个功能,据我所知,我试图实现。
function findAndRemove(array, property, value) {
$.each(array, function(index, result) {
if(result[property] == value) {
//Remove from array
array.splice(index, 1);
}
});
}
findAndRemove(Airports, 'type', 'heliport');
这可以用来处理,然后将其保存回我的airports.json文件?
答
请更改这样
function findAndRemove(array, property, value) {
$.each(array, function(index, result) {
if((typeof result !== 'undefined'))
if(result[property] == value) {
//Remove from array
array.splice(index, 1);
}
});
}
findAndRemove(Airports["Airports"], 'type', 'heliport');
相关代码:http://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes – k0pernikus