各类方法中涉及使用
1、filter
2、includes
3、indexOf
4、some()
1、去除数组ee中含数组cc中某值的对象,返还一个新数组 1
let returnlist = [],
ee=[{seat:1},{seat:0},{seat:2},{seat:3}],
cc=[{seat:1},{seat:4},{seat:5},{seat:3}]
returnlist = ee.filter(item=>{
let arrList = cc.map(it=>it.seat)
return !arrList.includes(item.seat)
})
console.log(returnlist)
打印结果
去除数组ee中含数组cc中某值的对象,返还一个新数组 2
let returnlist = [],
ee=[{seat:1},{seat:0},{seat:2},{seat:3}],
cc=[{seat:1},{seat:4},{seat:5},{seat:3}]
for(let i=0;i<ee.length;i++){
let exit = false
for(let s=0;s<cc.length;s++){
if(ee[i].seat == cc[s].seat){
exit = true
}
}
if(!exit){
returnlist.push(ee[i])
}
}
console.log(returnlist)
去除数组ee中含数组cc中某值的对象,返还一个新数组 3
let returnlist = [],
ee=[{seat:1},{seat:0},{seat:2},{seat:3}],
cc=[{seat:1},{seat:4},{seat:5},{seat:3}]
returnlist = ee.filter(item=>!cc.some(it=>it.seat===item.seat))
console.log(returnlist)
交集
let ee= [1,2,3,4,5],
cc = [5,6,7,8,9];
let returnlist = ee.filter(function (val) { return cc.indexOf(val) > -1 })
console.log(returnlist)
结果:
并集
const ee = [1,2,3,4,5],
cc = [5,6,7,8,9];
let returnlist = ee.concat(cc.filter(function (val) { return !(ee.indexOf(val) > -1) }))
console.log(returnlist)
有待更新