vue 两个数组判断是否有重复

vue 两个数组判断是否有重复,有几种判断方式?
let arr1 = ['runoob', 'google']
let arr2 = ['jingdong', 'google', 'taobao']

阅读 3.2k
2 个回答
let arr1 = ['runoob', 'google']
let arr2 = ['jingdong', 'google', 'taobao']

const newArr = [...arr1, ...arr2]
const uniqArr = [...new Set(newArr)]
if(newArr.length !== uniqArr.length) // 有重复

随便写两种

let arr1 = ['runoob', 'google']
let arr2 = ['jingdong', 'google', 'taobao']

//
console.log(arr1.some(r => arr2.includes(r)))

//
console.log(!arr1.every(r => !arr2.includes(r)))

//
console.log(arr1.filter(r => arr2.includes(r)).length)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题