var countries = {};
countries.results = [
{id:'AF',name:'Afghanistan'},
{id:'AL',name:'Albania'},
{id:'DZ',name:'Algeria'}
];
Array.prototype.removeValue = function(name, value){
var array = $.map(this, function(v,i){
return v[name] === value ? null : v;
});
this.length = 0; //clear original array
this.push.apply(this, array); //push all elements except the one we want to delete
}
說明:
countries.results.removeValue('name', 'Albania');
//移除掉key為'name' ,value為'Albania' 的那項item
Array.prototype.removeValue
//為Array這個object增加removeValue這個function.
var array = $.map(this, function(v,i){
return v[name] === value ? null : v;
});
//jquery的traversing object功能, callback function裡面
//可以程式來對object做動作,做完再回傳到呼叫他的地方.
this.push.apply(this,array)
//將array所有內容家到原本的array裡面,
你可以想成jabva的 addAll
總結:
說穿了其實就是把json array 用for-loop跑完,
將符合標準的取出.
最後再放回json array裡面
//===============================================================
ref:
http://api.jquery.com/jquery.map/
http://codex.wiki/post/147898-659/
http://stackoverflow.com/questions/1374126/how-to-extend-an-existing-javascript-array-with-another-array
廣告
2015年7月14日 星期二
[js] 移除jsonArray 當中某一項 | remove specific object from json arrray
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言