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

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

    Update @Paul Grimshaw answer, use includes insteed of indexOf for more readable

    let found = arr1.some(r=> arr2.indexOf(r) >= 0)
    let found = arr1.some(r=> arr2.includes(r))

提交回复
热议问题