使用其名称/值从数组中删除项目 [英] remove item from array using its name / value
本文介绍了使用其名称/值从数组中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下阵列
VAR国家= {};countries.results = [
{ID:AF,名称:'阿富汗'},
{ID:'AL',名称:阿尔巴尼亚},
{ID:DZ,名称:阿尔及利亚}
];
我怎样才能从这个数组使用其名称或ID删除一个项目?
感谢您
解决方案
Array.prototype.removeValue =功能(名称,值){
VAR阵列= $ .MAP(这一点,函数(V,I){
返回v [名] ===价值?空:伏;
});
this.length = 0; //清原数组
this.push.apply(这一点,数组); //推动所有元素,除了我们要删除的
}countries.results.removeValue('名','阿');
I have the following array
var countries = {};
countries.results = [
{id:'AF',name:'Afghanistan'},
{id:'AL',name:'Albania'},
{id:'DZ',name:'Algeria'}
];
How can I remove an item from this array using its name or id ?
Thank you
解决方案
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');
这篇关于使用其名称/值从数组中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文