I have two arrays:
我有两个数组:
var firstArr = [1,2,3,4,5];
var secondArr = [2,3];
How can I get an array like this:
我怎样才能获得这样的数组:
[1,4,5]
I would like to use a solution that could be used when elements of the array are objects with multiple properties.
我想使用一个解决方案,当数组的元素是具有多个属性的对象时,可以使用该解决方案。
2 个解决方案
#1
2
Use filter
:
The
filter()
method creates a new array with all elements that pass the test implemented by the provided function.filter()方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。
For example:
firstArr.filter(function(item){
return secondArr.indexOf(item) === -1;
});
#2
0
You can use this Function to remove items.
您可以使用此功能删除项目。
function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
var ary = ['three', 'seven', 'eleven'];
removeA(ary, 'seven');
#1
2
Use filter
:
The
filter()
method creates a new array with all elements that pass the test implemented by the provided function.filter()方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。
For example:
firstArr.filter(function(item){
return secondArr.indexOf(item) === -1;
});
#2
0
You can use this Function to remove items.
您可以使用此功能删除项目。
function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
var ary = ['three', 'seven', 'eleven'];
removeA(ary, 'seven');