我正在寻找一种有效的方法,从javascript数组中删除所有元素,如果它们存在于另一个数组中。
// If I have this array:
var myArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
// and this one:
var toRemove = ['b', 'c', 'g'];
我想对myArray进行操作,使其处于这种状态:['a', 'd', 'e', 'f']
与jQuery,我使用grep()和inArray(),这工作得很好:
myArray = $.grep(myArray, function(value) {
return $.inArray(value, toRemove) < 0;
});
有没有一个纯javascript的方法来做到这一点没有循环和剪接?
//Using the new ES6 Syntax
console.log(["a", "b", "c", "d", "e", "f", "g"].filter(el => !["b", "c", "g"].includes(el)));
// OR
// Main array
let myArray = ["a", "b", "c", "d", "e", "f", "g"];
// Array to remove
const toRemove = ["b", "c", "g"];
const diff = () => (myArray = myArray.filter((el) => !toRemove.includes(el)));
console.log(diff()); // [ 'a', 'd', 'e', 'f' ]
// OR
const diff2 = () => {
return myArray = myArray.filter((el) => !toRemove.includes(el));
};
console.log(diff2()); // [ 'a', 'd', 'e', 'f' ]
如果您正在使用对象数组。然后,下面的代码将发挥神奇的作用,其中对象属性将作为删除重复项的标准。
在下面的示例中,比较每个项目的名称,已删除重复项。
试试这个例子。http://jsfiddle.net/deepak7641/zLj133rh/
var myArray = [
{name: 'deepak', place: 'bangalore'},
{name: 'chirag', place: 'bangalore'},
{name: 'alok', place: 'berhampur'},
{name: 'chandan', place: 'mumbai'}
];
var toRemove = [
{name: 'deepak', place: 'bangalore'},
{name: 'alok', place: 'berhampur'}
];
for( var i=myArray.length - 1; i>=0; i--){
for( var j=0; j<toRemove.length; j++){
if(myArray[i] && (myArray[i].name === toRemove[j].name)){
myArray.splice(i, 1);
}
}
}
alert(JSON.stringify(myArray));
过滤方法应该做到这一点:
const myArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
const toRemove = ['b', 'c', 'g'];
// ES5 syntax
const filteredArray = myArray.filter(function(x) {
return toRemove.indexOf(x) < 0;
});
如果toRemove数组很大,这种查找模式可能效率很低。创建一个映射,使查找是O(1)而不是O(n),这样会更有性能。
const toRemoveMap = toRemove.reduce(
function(memo, item) {
memo[item] = memo[item] || true;
return memo;
},
{} // initialize an empty object
);
const filteredArray = myArray.filter(function (x) {
return toRemoveMap[x];
});
// or, if you want to use ES6-style arrow syntax:
const toRemoveMap = toRemove.reduce((memo, item) => ({
...memo,
[item]: true
}), {});
const filteredArray = myArray.filter(x => toRemoveMap[x]);
//Using the new ES6 Syntax
console.log(["a", "b", "c", "d", "e", "f", "g"].filter(el => !["b", "c", "g"].includes(el)));
// OR
// Main array
let myArray = ["a", "b", "c", "d", "e", "f", "g"];
// Array to remove
const toRemove = ["b", "c", "g"];
const diff = () => (myArray = myArray.filter((el) => !toRemove.includes(el)));
console.log(diff()); // [ 'a', 'd', 'e', 'f' ]
// OR
const diff2 = () => {
return myArray = myArray.filter((el) => !toRemove.includes(el));
};
console.log(diff2()); // [ 'a', 'd', 'e', 'f' ]
这已经很晚了,但添加这个来解释@mojtaba roohi的回答。第一个代码块将不起作用,因为每个数组都有不同的对象,即df[0] != nfl[2]。这两个对象看起来相似,但完全不同,当我们使用像数字这样的基本类型时,情况就不是这样了。
let df = [{'name': 'C'},{'name': 'D'}]
让nfl =[{'名字':' A '},{'名字':' B '},{'名字':' C '},{'名字':' D '})
Let res = nfl。filter(x => df.indexOf(x)<0)
console.log (res)
下面是工作代码:
let df = [{'name': 'C'},{'name': 'D'}]
让nfl =[{'名字':' A '},{'名字':' B '},{'名字':' C '},{'名字':' D '});
让res = nfl.filter ((o1群)= > ! df.some ((o2) = > o1.name = = = o2.name));
console.log (res)