我有一个数组,其中包含一个名称列表。如果这些名称与另一个数组中的键/值对匹配,我想删除包含匹配项的对象,并返回没有该对象的数组。
这就是我迄今为止的例子:
var supervisorsToRemove = ["Supervisor1", "Supervisor2"];
var data = [{id: "Name1", supervisor: "Supervisor1"},{id: "Name2", supervisor: "Supervisor1"},{id: "Name3", supervisor: "Supervisor2"},{id: "Name4", supervisor: "Supervisor3"}]
我希望得到的数据数组为:
data = [{id: "Name4", supervisor: "Supervisor3"}]
我可以使用以下内容删除一个主管的对象:
var supervisorsToRemove = "Supervisor1";
data = $.grep(data, function(e){
return e.supervisor != supervisorsToRemove;
});
但是当我尝试删除数组中的所有内容时,它不起作用:
var supervisorsToRemove = ["Supervisor1","Supervisor2"];
data = $.grep(data, function(e){
return e.supervisor != supervisorsToRemove;
});
答案 0 :(得分:4)
使用Array#indexOf
方法(或Array#includes
方法)检查数组中存在的元素
data = $.grep(data, function(e){
return supervisorsToRemove.indexOf(e.supervisor) == -1;
// or return supervisorsToRemove.includes(e.supervisor);
});
var supervisorsToRemove = ["Supervisor1", "Supervisor2"];
var data = [{
id: "Name1",
supervisor: "Supervisor1"
}, {
id: "Name2",
supervisor: "Supervisor1"
}, {
id: "Name3",
supervisor: "Supervisor2"
}, {
id: "Name4",
supervisor: "Supervisor3"
}]
console.log($.grep(data, function(e) {
return supervisorsToRemove.indexOf(e.supervisor) == -1;
// or return supervisorsToRemove.includes(e.supervisor);
}));

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
更好的方法是使用一个将超级用户值保存为属性的对象,这有助于使其更快,因为Array#indexOf
方法较慢。
var supervisorsToRemove = {"Supervisor1" : true, "Supervisor2" : true };
data = $.grep(data, function(e){
return !supervisorsToRemove[e.supervisor];
});
var data = [{
id: "Name1",
supervisor: "Supervisor1"
}, {
id: "Name2",
supervisor: "Supervisor1"
}, {
id: "Name3",
supervisor: "Supervisor2"
}, {
id: "Name4",
supervisor: "Supervisor3"
}]
var supervisorsToRemove = {
"Supervisor1": true,
"Supervisor2": true
};
console.log(
$.grep(data, function(e) {
return !supervisorsToRemove[e.supervisor];
})
);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
如果您无法控制数组,请使用Array#reduce
方法生成对象。
var obj = supervisorsToRemove.reduce(function(obj, v){ obj[v] = true; return obj; }, {});
data = $.grep(data, function(e){
return !obj[e.supervisor];
});
var supervisorsToRemove = ["Supervisor1", "Supervisor2"];
var data = [{
id: "Name1",
supervisor: "Supervisor1"
}, {
id: "Name2",
supervisor: "Supervisor1"
}, {
id: "Name3",
supervisor: "Supervisor2"
}, {
id: "Name4",
supervisor: "Supervisor3"
}]
var supervisorsToRemove = {
"Supervisor1": true,
"Supervisor2": true
};
var obj = supervisorsToRemove.reduce(function(obj, v) {
obj[v] = true;
return obj;
}, {});
console.log(
$.grep(data, function(e) {
return !obj[e.supervisor];
})
);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
您甚至可以使用原生Javascript Array#filter
方法。
var supervisorsToRemove = ["Supervisor1", "Supervisor2"];
var data = [{
id: "Name1",
supervisor: "Supervisor1"
}, {
id: "Name2",
supervisor: "Supervisor1"
}, {
id: "Name3",
supervisor: "Supervisor2"
}, {
id: "Name4",
supervisor: "Supervisor3"
}]
var supervisorsToRemove = {
"Supervisor1": true,
"Supervisor2": true
};
console.log(
data.filter(function(e) {
return !supervisorsToRemove[e.supervisor];
})
);
&#13;
答案 1 :(得分:0)
在任何合理的现代浏览器中,都不需要jQuery。 filter()
将完成这项工作。
var supervisorsToRemove = ["Supervisor1", "Supervisor2"];
var data = [{
id: "Name1",
supervisor: "Supervisor1"
}, {
id: "Name2",
supervisor: "Supervisor1"
}, {
id: "Name3",
supervisor: "Supervisor2"
}, {
id: "Name4",
supervisor: "Supervisor3"
}]
var results = data.filter(function(o) {
return (supervisorsToRemove.indexOf(o.supervisor) < 0);
});
console.log(results);