我找到原因了:
内置的filter不好用,自己写了个forEach就可以了
let rows = [1, 2, 3, 4, 5, 6, 7, 8]
let a = rand()
console.log("a: ", a)
let b = rand()
console.log("b: ", b)
//let c = a.filter(item => b.indexOf(item) > -1)
let c = intersection(a, b)
console.log("intersection of a and b: ", c)
function rand() {
return rows.filter(() => Math.random() > 0.5)
}
function intersection(a, b) {
let c = []
a.forEach(item => { if (b.indexOf(item) > -1) { c.push(item) } })
return c
}
WPS函数专家