请问大家一个小算法问题
var arr1 = ['27709', '29406', '29407', '29408', '29409', '29410', '29411', '29412', '29414', '29415']
var arr2 = ['27709', '29406', '29409', '29410', '29411', '29412', '29414', '29415']
上面两个数组,arr1里有10个,arr2里有8个,请问如何找出包含arr2中的,多出来的两个值,谢谢大家抽空帮忙解答
回答:
是找出差值 不包含的吧
let newArr = arr1.filter(item=>!arr2.includes(item))
回答:
直接上菜
function subArr(a, b) { return a.filter(i => b.indexOf(i) == -1)
}
const arr1 = ['27709', '29406', '29407', '29408', '29409', '29410', '29411', '29412', '29414', '29415']
const arr2 = ['27709', '29406', '29409', '29410', '29411', '29412', '29414', '29415']
console.log(subArr(arr1, arr2))
回答:
function partition() { var intersection = arguments[0], complement = [];
for (var i = 1; i < arguments.length; ++i) {
var temporary = [];
for (var j = 0; j < arguments[i].length; ++j) {
var item = arguments[i][j];
if (intersection.indexOf(item) >= 0) {
temporary.push(item);
} else if (complement.indexOf(item) < 0) {
complement.push(item);
}
}
intersection = temporary;
}
for (var i = arguments[0].length; i--;) {
var item = arguments[0][i];
if (intersection.indexOf(item) < 0 && complement.indexOf(item) < 0) {
complement.unshift(item);
}
}
return {
intersection: intersection,
complement: complement,
union: intersection.concat(complement)
};
}
var set = partition(arr1, arr2);
console.log(set.complement); // 补集
console.log(set.intersection); // 交集
console.log(set.union); // 并集
以上是 请问大家一个小算法问题 的全部内容, 来源链接: utcz.com/p/937417.html