jQuery基于另一个数组从数组中删除多个对象

时间:2022-09-25 07:54:36

I have an array, which contains a list of names. If those names match a key/value pair in another array, I'd like to remove the object containing the match and return the array without that object.

我有一个数组,其中包含一个名称列表。如果这些名称与另一个数组中的键/值对匹配,我想删除包含匹配项的对象,并返回没有该对象的数组。

Here's what I have so far as an example:

这是我到目前为止的例子:

var supervisorsToRemove = ["Supervisor1", "Supervisor2"];
var data = [{id: "Name1", supervisor: "Supervisor1"},{id: "Name2", supervisor: "Supervisor1"},{id: "Name3", supervisor: "Supervisor2"},{id: "Name4", supervisor: "Supervisor3"}]

I'd like the resulting data array to be:

我希望得到的数据数组是:

data = [{id: "Name4", supervisor: "Supervisor3"}]

I can remove one supervisor's objects with the following:

我可以使用以下内容删除一个主管的对象:

var supervisorsToRemove = "Supervisor1";
data = $.grep(data, function(e){ 
     return e.supervisor != supervisorsToRemove; 
});

But when I try to remove all in the array, it doesn't work:

但是当我尝试删除数组中的所有内容时,它不起作用:

var supervisorsToRemove = ["Supervisor1","Supervisor2"];
data = $.grep(data, function(e){ 
     return e.supervisor != supervisorsToRemove; 
});

2 个解决方案

#1


4  

Use Array#indexOf method(or Array#includes method) to check element present in an array

使用Array #indexOf方法(或Array #include方法)检查​​数组中存在的元素

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>


A better approach would be using an object which holds the supervisor value as property, that would help to make it faster since Array#indexOf method is slower one.

更好的方法是使用一个将supervisor值保存为属性的对象,这有助于使其更快,因为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];
  })
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

In case you don't have control over the array then generate the object using Array#reduce method.

如果您无法控制数组,则使用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];
  })
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


You can even use native Javascript Array#filter method.

您甚至可以使用本机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];
  })
);

#2


0  

In any reasonably-modern browser, no jQuery is needed. filter() will do the job.

在任何合理的现代浏览器中,都不需要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);

#1


4  

Use Array#indexOf method(or Array#includes method) to check element present in an array

使用Array #indexOf方法(或Array #include方法)检查​​数组中存在的元素

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>


A better approach would be using an object which holds the supervisor value as property, that would help to make it faster since Array#indexOf method is slower one.

更好的方法是使用一个将supervisor值保存为属性的对象,这有助于使其更快,因为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];
  })
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

In case you don't have control over the array then generate the object using Array#reduce method.

如果您无法控制数组,则使用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];
  })
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


You can even use native Javascript Array#filter method.

您甚至可以使用本机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];
  })
);

#2


0  

In any reasonably-modern browser, no jQuery is needed. filter() will do the job.

在任何合理的现代浏览器中,都不需要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);