在 jQuery 中从数组中删除匹配的元素
remove matched elements from the array in jquery
>我有一个类似
userlist=[
{
"label": "veera_ss.com",
"value": "veera_ss.com"
},
{
"label": "v_v.com",
"value": "v_v.com"
},
{
"label": "sample_vh.com",
"value": "sample_vh.com"
},
{
"label": "sample_vh.com",
"value": "sample_vh.com"
},
{
"label": "nabeel.ahameed_vh.com",
"value": "nabeel.ahameed_vh.com"
}
]
我需要遍历此并从此列表中删除匹配的元素,例如,如果值v_v.com,sample_vh.com
将从列表中删除
userlist=[
{
"label": "veera_ss.com",
"value": "veera_ss.com"
},
{
"label": "sample_vh.com",
"value": "sample_vh.com"
},
{
"label": "nabeel.ahameed_vh.com",
"value": "nabeel.ahameed_vh.com"
}
]
在这种情况下,
您可以使用$.grep()
来实现您想要的。
尝试
var userlist =[{"label":"veera_ss.com","value":"veera_ss.com"},{"label":"v_v.com","value":"v_v.com"},{"label":"sample_vh.com","value":"sample_vh.com"},{"label":"sample_vh.com","value":"sample_vh.com"},{"label":"nabeel.ahameed_vh.com","value":"nabeel.ahameed_vh.com"}]
userlist = $.grep(userlist, function(val){
return val.label !== 'v_v.com' && val.label !== 'sample_vh.com'
}).get(0);
你不需要jquery。Javascript数组有一个方法:filter。您可以像这样使用它:
[{"label":"veera_ss.com","value":"veera_ss.com"},
{"label":"v_v.com","value":"v_v.com"},
{"label":"sample_vh.com","value":"sample_vh.com"},
{"label":"sample_vh.com","value":"sample_vh.com"},
{"label":"nabeel.ahameed_vh.com","value":"nabeel.ahameed_vh.com"}]
.filter(function (el) {
return el.label !== 'v_v.com' && el.label !== 'sample_vh.com'
});
你可以
试试这种方式——
var arr = []; // will hold final array after removing elements
$.each(userlist,function(v){
if(v.value != "sample_vh.com" && v.value != "sample_vh.com")
arr.push(v);
});
你可以像这样使用下划线拒绝:
var userlist=[{"label":"veera_ss.com","value":"veera_ss.com"},{"label":"v_v.com","value":"v_v.com"},{"label":"sample_vh.com","value":"sample_vh.com"},{"label":"sample_vh.com","value":"sample_vh.com"},{"label":"nabeel.ahameed_vh.com","value":"nabeel.ahameed_vh.com"}];
userlist = _.reject(userlist, function(user) {
return user.value=="veera_ss.com" || user.value=="v_v.com";
});
创建一个新列表并进行比较
var userlist=[{"label":"veera_ss.com","value":"veera_ss.com"},
{"label":"v_v.com","value":"v_v.com"},
{"label":"sample_vh.com","value":"sample_vh.com"},
{"label":"sample_vh.com","value":"sample_vh.com"},
{"label":"nabeel.ahameed_vh.com","value":"nabeel.ahameed_vh.com"}]
var newList = []
userlist.forEach(function(item, key) {
if(!newList.some(function(x){
return JSON.stringify(x) === JSON.stringify(item) }))
{
newList.push(item);
}
})
//newList is filtered result.
相关文章:
- 在函数中添加数组元素的数值
- 访问JSON对象内部的数组元素
- 用于检查数组中是否存在元素的javascript自定义方法
- 在数组中的一个元素上设置多个值
- 从组件状态的数组中删除元素
- Mongoose-在更新中删除数组元素
- 从数组中删除元素的最佳方法是:javascript/jquery
- 数组中元素(字符串)的总和
- javascript数组元素是否知道其封闭数组
- 查找数组javascript中包含的元素类型
- 如何在javascript上向数组的对象添加新元素
- 如果数组仍在执行,async.forEach()将运行我添加到数组中的新元素
- 使用filter和map方法将数组中某些元素的第一个字母大写-JavaScript
- 一些元素没有从数组中删除
- JS.循环遍历多维数组,以计数元素在每列中的出现次数
- 将数组元素附加到FormData dos'不适用于Firefox 15
- 元素不会添加到Javascript数组中
- 如何从另一个带下划线的数组中筛选带元素的数组
- 如何将函数应用于两个数组的第 n 个元素(数组的数组)
- Js接受两个值之间的元素(数组)