const serveralArr = [
[1,2,4,5,23,3,2,2,4,3,5,5],
[3,2,3,2,2,4,3,1,4,5,6],
[3,2,4,3,2,4,1,2,5],
[3,2,4,5,5,4,3,1,2,2],
[3,2,23,3,4,1,3,4,5,5,4,3,1,2,2],
[3,2,4,1,2,5,5,4,3,1,2,2],
[3,2,4,25,5,4,3,1,2,2],
]
// ES5 方法实现数学意义上的交集结果
const intersectNoRepeatFirst = serveralArr =>{
let minArr = serveralArr[0]
serveralArr.forEach(item =>{
if(item.length <minArr.length){
minArr = item
}
})
const result = []
minArr.forEach(item =>{
serveralArr.forEach(j =>{
if(j.includes(item) &&!result.includes(item)){
result.push(item)
}
})
})
return result
}
// ES6 方法实现数学意义上的交集结果
const intersectNoRepeatTwice = arrs =>{
return arrs.reduce(function(prev,cur){
// return [...new Set(cur.filter((item)=>prev.includes(item)))]
return Array.from(new Set(cur.filter((item)=>prev.includes(item))))
})
}
// 输出数学意义上的交集结果
console.log(intersectNoRepeatFirst(serveralArr), intersectNoRepeatTwice(serveralArr)) // =>(5) [3, 2, 4, 1, 5](5) [3, 2, 4, 5, 1]
// ES5 方法实现有重复元素的交集结果
const intersectRepeat = arr =>{
const result = []
const tempArr = []
arr.forEach(item =>{
const obj = {}
item.forEach(j =>{
if(obj.hasOwnProperty(j)){
obj[j] += 1
}else{
obj[j] = 1
}
})
tempArr.push(obj)
})
let arrr = tempArr[0]
tempArr.forEach(item =>{
if(item.length <arrr.length){
arrr = item
}
})
const newOjb = {}
Object.keys(arrr).forEach(item =>{
newOjb[item] = Math.min.apply(Math, tempArr.map(function(o) {return o[item]}))
})
Object.keys(newOjb).forEach(item =>{
if(newOjb[item]){
for(let i = 0i <newOjb[item]i++){
result.push(Number(item))
}
}
})
return result
}
// 输出有重复元素的交集结果
console.log(intersectRepeat(serveralArr)) // =>(9) [1, 2, 2, 2, 3, 3, 4, 4, 5]
相同的a.filter(function(v){returnb.indexOf(v)>-1})不同的 a.filter(function(v){ return !(b.indexOf(v) >-1) }).concat(b.filter(function(v){ return !(a.indexOf(v) >-1)}))最小的y坐标或者最大的y坐标是否在另一个div的俩y坐标之间,最小的x坐标或者最大的x坐标是否在另一个x坐标之间,
列如:
div1={x11,x12,y11,y12}
div2={x21,x22,y21,y22}
if(((x21<x11&&x11<x22)||(x21<x12&&x12<x22))&&((y21<y11&&y11<y22)||(y21<y12&&y12<y22))){
console.log(有交集)
}
这个是很笨的方法,,希望你有更好的思路