我正在寻找一种有效的方法,从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的方法来做到这一点没有循环和剪接?
过滤方法应该做到这一点:
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]);
过滤方法应该做到这一点:
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]);
ECMAScript 6集合可以更快地计算一个数组中不在另一个数组中的元素:
const myArray = [a, b, c, d, e, f, g的);
const toRemove = new Set(['b', 'c', 'g']);
const difference = myArray。toRemove.has(x));
console.log(差异);// ["a", "d", "e", "f"]
因为现在V8引擎浏览器使用的查找复杂度是O(1),所以整个算法的时间复杂度是O(n)。
删除另一个数组中包含的所有元素的正确方法是通过只删除元素来使源数组成为相同的对象:
Array.prototype.removeContained = function(array) {
var i, results;
i = this.length;
results = [];
while (i--) {
if (array.indexOf(this[i]) !== -1) {
results.push(this.splice(i, 1));
}
}
return results;
};
或CoffeeScript等价:
Array.prototype.removeContained = (array) ->
i = @length
@splice i, 1 while i-- when array.indexOf(@[i]) isnt -1
在chrome开发工具内测试:
19:33:04.447 = 1
19:33:06.354 b = 2
19:33:07.615 c = 3
19:33:09.981 arr = [a,b,c]
19:33:16.460 arr1 = arr
19:33:20.317 arr1 === arr
19:33:20.331真实
19:33:43.592 arr.removeContained ([c])
19:33:52.433 arr === arr1
19:33:52.438真实
使用Angular框架是在更新集合时保持指针指向源对象的最佳方法,而不需要大量的监视器和重载。
你可以使用_。by和lodash的区别
const myArray = [
{name: 'deepak', place: 'bangalore'},
{name: 'chirag', place: 'bangalore'},
{name: 'alok', place: 'berhampur'},
{name: 'chandan', place: 'mumbai'}
];
const toRemove = [
{name: 'deepak', place: 'bangalore'},
{name: 'alok', place: 'berhampur'}
];
const sorted = _.differenceBy(myArray, toRemove, 'name');
示例代码:CodePen
//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' ]