js Array 交集 并集 差集 去重
最劲项目需要用到js数组去重和交集的一些运算,我的数组元素个数可能到达1000以上,网上的实现方式都是2次循环,性能不适合我的需求,1000*1000那循环次数太多了,所以我这里采用对象object来做处理,用空间换时间,code 如下:
// 集合取交集
Array.intersect = function () {
var result = new Array();
var obj = {};
for (var i = 0; i < arguments.length; i++) {
for (var j = 0; j < arguments[i].length; j++) {
var str = arguments[i][j];
if (!obj[str]) {
obj[str] = 1;
}
else {
obj[str]++;
if (obj[str] == arguments.length)
{
result.push(str);
}
}//end else
}//end for j
}//end for i
return result;
}
// 集合去掉重复
Array.prototype.uniquelize = function () {
var tmp = {},
ret = [];
for (var i = 0, j = this.length; i < j; i++) {
if (!tmp[this[i]]) {
tmp[this[i]] = 1;
ret.push(this[i]);
}
}
return ret;
}
// 并集
Array.union = function () {
var arr = new Array();
var obj = {};
for (var i = 0; i < arguments.length; i++) {
for (var j = 0; j < arguments[i].length; j++)
{
var str=arguments[i][j];
if (!obj[str])
{
obj[str] = 1;
arr.push(str);
}
}//end for j
}//end for i
return arr;
}
// 2个集合的差集 在arr不存在
Array.prototype.minus = function (arr) {
var result = new Array();
var obj = {};
for (var i = 0; i < arr.length; i++) {
obj[arr[i]] = 1;
}
for (var j = 0; j < this.length; j++) {
if (!obj[this[j]])
{
obj[this[j]] = 1;
result.push(this[j]);
}
}
return result;
};
console.log(Array.intersect(["1", "2", "3"], ["2", "3", "4", "5", "6"]));//[2,3]
console.log([1, 2, 3, 2, 3, 4, 5, 6].uniquelize());//[1,2,3,4,5,6]
console.log(Array.union(["1", "2", "3"], ["2", "3", "4", "5", "6"], ["5", "6", "7", "8", "9"]))
console.log(["2", "3", "4", "5", "6"].minus(["1", "2", "3"]));
js 数组 : 差集、并集、交集、去重、多维转一维
原文链接
对象转数组
// input: [{name:'liujinyu'},{name:'noah'}].choose('name');
// output: ['liujinyu','noah']
Array.prototype.choose = function(key){
var tempArr = [];
this.forEach(function(v){
tempArr.push(v[key])
});
return tempArr;
}
并集
方法1:
Array.prototype.union = function(arr){
var tempArr = this.slice();
arr.forEach(function(v) {
!tempArr.includes(v) && tempArr.push(v)
});
return tempArr;
}
// [1,2,3].union([3,4]) output:[1,2,3,4]
方法2:
var a = [1,2,3],b = [3,4];
var tempArr = a.slice();
b.forEach(v => {!tempArr.includes(v) && tempArr.push(v)});
console.log("tempArr",tempArr); // [1,2,3,4]
差集
var a = [1,2,3],b = [3,4];
a.concat(b).filter(v => a.includes(v) ^ b.includes(v)); // [1,2,4]
交集
var a = [1,2,3],b = [3,4];
a.filter(v => b.includes(v)); // [3]
去重
方法1:
var tempArr = [] ;
arr.filter(v => tempArr.includes(v) ? false : tempArr.push(v) )
方法2:
var arr = [1, 2, 2, 4, 4];
// 使用 Set 对象将重复的去掉
var mySet = new Set(arr);
console.log(mySet); // Set(3) {1, 2, 4}
// 然后将set对象转变为数组
// 方法1,使用 Array.from 转变为数组
// var arr = Array.from(mySet); // [1, 2, 4]
// 方法2,使用 spread 操作符
var arr = [...mySet]; // [1, 2, 4]
// 方法3, 传统forEach
var arr2 = [];
mySet.forEach(v => arr2.push(v) );
多维转一维
var arr = [1,[2,[[3,4],5],6]];
function unid(arr){
var arr1 = (arr + '').split(',');//将数组转字符串后再以逗号分隔转为数组
var arr2 = arr1.map(function(x){
return Number(x);
});
return arr2;
}
console.log(unid(arr)); // [1, 2, 3, 4, 5, 6]