数组交集,并集,差集
//一维数组和多位数组交集差集
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)
//ES6一维数组
let arr1 = [1,2,3,4,5,6], arr2 = [2,4,6,8,10]; //并集 function bj(arr1,arr2){ return [...new Set([...arr1,...arr2])]; } console.log(bj(arr1,arr2)); //交集
let jj1 = arr1.filter(value => arr2.includes(value)) function jj(arr1,arr2){ return arr1.filter((item)=>{ return arr2.includes(item); }); } console.log(jj(arr1,arr2)); //差集 function cj(arr1,arr2){ return bj(arr1,arr2).filter((item)=>{ return !(jj(arr1,arr2).includes(item)) }); } console.log(cj(arr1,arr2));
//交集取权限
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)
数组去重
function unique(ary){ for(let i=0;i<ary.length-1;i++){ let cur = ary[i] for(let j=i+1;j<ary.length;j++){ if(cur===ary[j]){ ary.splice(j,1) j-- } } } return ary } function unique1(ary){ let obj = {}; for(let i=0;i<ary.length;i++){ let cur = ary[i]; if(typeof obj[cur] !== 'undefined'){ ary[i] = ary[ary.length - 1]; ary.length--; i--; continue; } obj[cur] = cur; } return ary } console.log(unique([1,5,4,6,5,4,2,3,1,1])) var ary = [{id:1,name:'123'},{id:2,name:'234'},{id:1,name:'345'}] var list = [] $(ary).each(function(index,item){ var cur = item.id if(list.length > 0){ var flag = 0 for(var i=0;i<list.length;i++){ if(list[i].id == cur){ flag = 1 } } if(flag==0){ list.push(item) } }else{ list.push(item) } }) console.log(list)
//遍历对象 let obj = {a:1,b:2,c:3} Object.keys(obj).forEach(name=>{ console.log(name,obj[name]) })
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界