是否有一种方法可以在JavaScript中返回两个数组之间的差异?
例如:
var a1 = ['a', 'b'];
var a2 = ['a', 'b', 'c', 'd'];
// need ["c", "d"]
是否有一种方法可以在JavaScript中返回两个数组之间的差异?
例如:
var a1 = ['a', 'b'];
var a2 = ['a', 'b', 'c', 'd'];
// need ["c", "d"]
当前回答
function array_diff(array1, array2) {
let returnArray = [];
$.each(array1, function(index, value) {
let findStatus = false;
if (Array.isArray(array2)) {
$.each(array2, function(index2, value2) {
if (value == value2) findStatus = true;
});
} else {
if (value == array2) {
findStatus = true;
}
}
if (findStatus == false) {
returnArray.push(value);
}
});
return returnArray;
}
其他回答
function diffArray(arr1, arr2) {
const newArr = [];
// arr1 match to arr2
arr1.map((item)=>{
if(arr2.indexOf(item)<0){
console.log(item)
newArr.push(item)
}
})
// arr2 match to arr1
arr2.map((item)=>{
if(arr1.indexOf(item)<0){
newArr.push(item)
}
})
return newArr;
}
差异([1,2,3,5],[1,2,3,4,5])
输出::[4]
我想要一个类似的函数,它接受一个旧数组和一个新数组,并给我一个添加项的数组和一个删除项的数组,我希望它是有效的(所以没有.contains!)。
你可以在这里尝试我提出的解决方案:http://jsbin.com/osewu3/12。
有人能看出算法的问题或改进吗?谢谢!
代码清单:
function diff(o, n) {
// deal with empty lists
if (o == undefined) o = [];
if (n == undefined) n = [];
// sort both arrays (or this won't work)
o.sort(); n.sort();
// don't compare if either list is empty
if (o.length == 0 || n.length == 0) return {added: n, removed: o};
// declare temporary variables
var op = 0; var np = 0;
var a = []; var r = [];
// compare arrays and add to add or remove lists
while (op < o.length && np < n.length) {
if (o[op] < n[np]) {
// push to diff?
r.push(o[op]);
op++;
}
else if (o[op] > n[np]) {
// push to diff?
a.push(n[np]);
np++;
}
else {
op++;np++;
}
}
// add remaining items
if( np < n.length )
a = a.concat(n.slice(np, n.length));
if( op < o.length )
r = r.concat(o.slice(op, o.length));
return {added: a, removed: r};
}
这个怎么样:
Array.prototype.contains = function(needle){
for (var i=0; i<this.length; i++)
if (this[i] == needle) return true;
return false;
}
Array.prototype.diff = function(compare) {
return this.filter(function(elem) {return !compare.contains(elem);})
}
var a = new Array(1,4,7, 9);
var b = new Array(4, 8, 7);
alert(a.diff(b));
这样你就可以用array1。diff(array2)来得到它们的区别算法的时间复杂度很可怕- O(array1。长度(我相信)
如果不使用hasOwnProperty,那么我们有不正确的元素。例如:
[1,2,3].diff([1,2]); //Return ["3", "remove", "diff"] This is the wrong version
我的版本:
Array.prototype.diff = function(array2)
{
var a = [],
diff = [],
array1 = this || [];
for (var i = 0; i < array1.length; i++) {
a[array1[i]] = true;
}
for (var i = 0; i < array2.length; i++) {
if (a[array2[i]]) {
delete a[array2[i]];
} else {
a[array2[i]] = true;
}
}
for (var k in a) {
if (!a.hasOwnProperty(k)){
continue;
}
diff.push(k);
}
return diff;
}
如果你的数组包含对象,如果你想比较一个属性,就会变得有点困难。
幸运的是,lodash使用_contains和_.pluck使这非常简单:
var list1 = [{id: 1},{id: 2}];
var list1 = [{id: 1},{id: 2}, {id: 3}];
//es6
var results = list2.filter(item => {
return !_.contains(_.pluck(list1, 'id'), item.id);
});
//es5
var results = list2.filter(function(item){
return !_.contains(_.pluck(list1, 'id'), item.id);
});
//results contains [{id: 3}]