Check if an array contains any element of another array in JavaScript

后端 未结 26 1148
礼貌的吻别
礼貌的吻别 2020-11-22 08:48

I have a target array [\"apple\",\"banana\",\"orange\"], and I want to check if other arrays contain any one of the target array elements.

For example:

26条回答
  •  攒了一身酷
    2020-11-22 09:09

    Just one more solution

    var a1 = [1, 2, 3, 4, 5]
    var a2 = [2, 4]
    

    Check if a1 contain all element of a2

    var result = a1.filter(e => a2.indexOf(e) !== -1).length === a2.length
    console.log(result)
    

提交回复
热议问题