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

后端 未结 26 1508
礼貌的吻别
礼貌的吻别 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:01

    You could use lodash and do:

    _.intersection(originalTarget, arrayToCheck).length > 0
    

    Set intersection is done on both collections producing an array of identical elements.

提交回复
热议问题