2015-11-01 28 views
0

我有一個龐大的機場列表,我打算用於自動完成目的。從文件(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文件?

+0

相關代碼:http://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes – k0pernikus

回答

0

請更改這樣

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'); 
相關問題