如果需要判断 a 数组中的某一项是否不存在于 b 数组中,可以使用以下几种方式:
1. indexOf + includes
const a = [1, 2, 3];
const b = [2, 3, 4];
if(b.indexOf(a[0]) === -1) {
// 不存在
}
2. every + includes
const a = [1, 2, 3];
const b = [2, 3, 4];
if(!b.every(item => a.includes(item))) {
// 不存在
}
3. some + !includes
const a = [1, 2, 3];
const b = [2, 3, 4];
if(a.some(item => !b.includes(item))) {
// 不存在
}
4. find + !includes
const a = [1, 2, 3];
const b = [2, 3, 4];
if(a.find(item => !b.includes(item))) {
// 不存在
}
5. Set + has
const a = [1, 2, 3];
const b = [2, 3, 4];
const setB = new Set(b);
if(!setB.has(a[0])) {
// 不存在
}