美文网首页
Javascript Array

Javascript Array

作者: 殷川 | 来源:发表于2021-09-24 18:37 被阅读0次

    数组常用方法

    面试题

    求两个数组的交集和差集
    交集
    const arr1 = [1, 3, 6, 8, 9]
    const arr2 = [9, 3]
    const result = arr1.filter((v) => arr2.includes(v))
    console.log(result) // [3,9]
    
    差集
    const arr1 = [1, 3, 6, 8, 9]
    const arr2 = [9, 3]
    const result2 = arr1.concat(arr2).filter((v) => !arr1.includes(v) || !arr2.includes(v))
    console.log(result) // [1,6,8]
    

    相关文章

      网友评论

          本文标题:Javascript Array

          本文链接:https://www.haomeiwen.com/subject/jcwzgltx.html