//一维数组和多位数组交集差集
var obj = [
{id:1,name:'a'},
{id:2,name:'b'},
{id:3,name:'c'},
{id:4,name:'d'},
{id:5,name:'e'},
{id:6,name:'f'},
{id:7,name:'g'},
{id:8,name:'h'},
{id:9,name:'i'}
]
var ary = [1,3,5,7,9];
var temp1 = [];
var temp2 = [];
$(obj).each(function(index,item){
var id = item.id;
//交集
for(var i=0;i<ary.length;i++){
if(id == ary[i]){
temp1.push(item);
}
}
//差集
if (ary.indexOf(id) < 0) {
temp2.push(item);
}
})
console.log(temp1,temp2)
//交集取权限
var obj = [
{id:1,name:'a',child:[{id:11,name:'a1'},{id:12,name:'a2'},{id:13,name:'a3'}]},
{id:2,name:'b',child:[{id:21,name:'b1'},{id:22,name:'b2'},{id:23,name:'b3'}]},
{id:3,name:'c',child:[{id:31,name:'c1'},{id:32,name:'c2'},{id:33,name:'c3'}]},
{id:4,name:'d',child:[{id:41,name:'d1'},{id:42,name:'d2'},{id:43,name:'d3'}]},
{id:5,name:'e',child:[{id:51,name:'e1'},{id:52,name:'e2'},{id:53,name:'e3'}]},
{id:6,name:'f',child:[{id:61,name:'f1'},{id:62,name:'f2'},{id:63,name:'f3'}]},
{id:7,name:'g',child:[{id:71,name:'g1'},{id:72,name:'g2'},{id:73,name:'g3'}]},
{id:8,name:'h',child:[{id:81,name:'h1'},{id:82,name:'h2'},{id:83,name:'h3'}]},
{id:9,name:'i',child:[{id:91,name:'i1'},{id:92,name:'i2'},{id:93,name:'i3'}]}
]
var ary2 = [1,11,13,2,23,4,42,6,61,7,72,73,9,91,92,93];
var temp3 = [];
$(obj).each(function(index,item){
$(ary2).each(function(i,t){
if(item.id == t){
var ary = [];
$(item.child).each(function(a,b){
$(ary2).each(function(c,d){
if(b.id == d){
ary.push(b)
}
})
})
//原始数据被改变了
item.child = ary;
temp3.push(item);
}
})
})
console.log(temp3)
var obj = [
{id:1,name:'a',child:[{id:11,name:'a1'},{id:12,name:'a2'},{id:13,name:'a3'}]},
{id:2,name:'b',child:[{id:21,name:'b1'},{id:22,name:'b2'},{id:23,name:'b3'}]},
{id:3,name:'c',child:[{id:31,name:'c1'},{id:32,name:'c2'},{id:33,name:'c3'}]},
{id:4,name:'d',child:[{id:41,name:'d1'},{id:42,name:'d2'},{id:43,name:'d3'}]},
{id:5,name:'e',child:[{id:51,name:'e1'},{id:52,name:'e2'},{id:53,name:'e3'}]},
{id:6,name:'f',child:[{id:61,name:'f1'},{id:62,name:'f2'},{id:63,name:'f3'}]},
{id:7,name:'g',child:[{id:71,name:'g1'},{id:72,name:'g2'},{id:73,name:'g3'}]},
{id:8,name:'h',child:[{id:81,name:'h1'},{id:82,name:'h2'},{id:83,name:'h3'}]},
{id:9,name:'i',child:[{id:91,name:'i1'},{id:92,name:'i2'},{id:93,name:'i3'}]}
]
var ary2 = [1,11,13,2,23,4,42,6,61,7,72,73,9,91,92,93];
var temp1 = [];
$.each(obj,function(index,item){
if(ary2.indexOf(item.id) >= 0){
var temp = [];
$.each(item.child,function(i,t){
if(ary2.indexOf(t.id) >= 0){
temp.push(t)
}
})
item.child = temp;
temp1.push(item)
}
})
console.log(temp1)
var obj = [
{id:1,name:'a',child:[{id:11,name:'a1'},{id:12,name:'a2'},{id:13,name:'a3'}]},
{id:2,name:'b',child:[{id:21,name:'b1'},{id:22,name:'b2'},{id:23,name:'b3'}]},
{id:3,name:'c',child:[{id:31,name:'c1'},{id:32,name:'c2'},{id:33,name:'c3'}]},
{id:4,name:'d',child:[{id:41,name:'d1'},{id:42,name:'d2'},{id:43,name:'d3'}]},
{id:5,name:'e',child:[{id:51,name:'e1'},{id:52,name:'e2'},{id:53,name:'e3'}]},
{id:6,name:'f',child:[{id:61,name:'f1'},{id:62,name:'f2'},{id:63,name:'f3'}]},
{id:7,name:'g',child:[{id:71,name:'g1'},{id:72,name:'g2'},{id:73,name:'g3'}]},
{id:8,name:'h',child:[{id:81,name:'h1'},{id:82,name:'h2'},{id:83,name:'h3'}]},
{id:9,name:'i',child:[{id:91,name:'i1'},{id:92,name:'i2'},{id:93,name:'i3'}]}
]
var ary2 = [1,11,13,2,23,4,42,6,61,7,72,73,9,91,92,93];
for(var i=0;i<obj.length;i++){
if(ary2.indexOf(obj[i].id) < 0){
obj.splice(i,1);
i--;
}else{
var temp = obj[i].child;
for(var j=0;j<temp.length;j++){
if(ary2.indexOf(temp[j].id) < 0){
temp.splice(j,1);
j--;
}
}
}
}
console.log(obj)
// 两个二维数组并集
var ary1 = [ {ind:1,name:'11'}, {ind:2,name:'22'}, {ind:3,name:'33'} ] var ary2 = [ {ind:2,name:'22'}, {ind:3,name:'33'}, {ind:5,name:'55'} ] var ary = ary1.concat(ary2); var res = new Map(); var resAry = ary.filter(function(a){ return !res.has(a.ind) && res.set(a.ind, 1) }) console.log(resAry)
//两个二维数组取交集
let b = [ {name:"张三",id:'11'}, {name:"李四",id:'12'}, {name:"王五",id:'13'}, {name:"隔壁老张2",id:'58'} ] let a = [ {name:"哈哈哈",id:''}, {name:"张三",id:'11'}, {name:"李四",id:'12'}, {name:"王五",id:'13'}, {name:"隔壁老张",id:'14'}, {name:"隔壁老张1",id:'19'}, {name:"隔壁老张2",id:'58'} ] for(let i=0;i<a.length;i++){ // if(a[i].id !== ''){ var c = b.some(item => item.id === a[i].id) if(!c){ a.splice(i,1) i-- } // } } let c = a.filter((item,index,arr) => { return b.find(it => it.id === item.id) // return item.id === "" || b.find(it => it.id === item.id) }) console.log(a) console.log(c)