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

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

    ES6 (fastest)

    const a = ['a', 'b', 'c'];
    const b = ['c', 'a', 'd'];
    a.some(v=> b.indexOf(v) !== -1)
    

    ES2016

    const a = ['a', 'b', 'c'];
    const b = ['c', 'a', 'd'];
    a.some(v => b.includes(v));
    

    Underscore

    const a = ['a', 'b', 'c'];
    const b = ['c', 'a', 'd'];
    _.intersection(a, b)
    

    DEMO: https://jsfiddle.net/r257wuv5/

    jsPerf: https://jsperf.com/array-contains-any-element-of-another-array

    0 讨论(0)
  • 2020-11-22 09:09
    const areCommonElements = (arr1, arr2) => {
        const arr2Set = new Set(arr2);
        return arr1.some(el => arr2Set.has(el));
    };
    

    Or you can even have a better performance if you first find out which of these two arrays is longer and making Set out for the longest array, while applying some method on the shortest one:

    const areCommonElements = (arr1, arr2) => {
        const [shortArr, longArr] = (arr1.length < arr2.length) ? [arr1, arr2] : [arr2, arr1];
        const longArrSet = new Set(longArr);
        return shortArr.some(el => longArrSet.has(el));
    };
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-22 09:09

    My solution applies Array.prototype.some() and Array.prototype.includes() array helpers which do their job pretty efficient as well

    ES6

    const originalFruits = ["apple","banana","orange"];
    
    const fruits1 = ["apple","banana","pineapple"];
    
    const fruits2 = ["grape", "pineapple"];
    
    const commonFruits = (myFruitsArr, otherFruitsArr) => {
      return myFruitsArr.some(fruit => otherFruitsArr.includes(fruit))
    }
    console.log(commonFruits(originalFruits, fruits1)) //returns true;
    console.log(commonFruits(originalFruits, fruits2)) //returns false;

    0 讨论(0)
  • 2020-11-22 09:09
    console.log("searching Array: "+finding_array);
    console.log("searching in:"+reference_array);
    var check_match_counter = 0;
    for (var j = finding_array.length - 1; j >= 0; j--) 
    {
        if(reference_array.indexOf(finding_array[j]) > 0)
        {
            check_match_counter = check_match_counter + 1;
        }
    }
     var match = (check_match_counter > 0) ? true : false;
    console.log("Final result:"+match);
    
    0 讨论(0)
  • 2020-11-22 09:11

    ES6 solution:

    let arr1 = [1, 2, 3];
    let arr2 = [2, 3];
    
    let isFounded = arr1.some( ai => arr2.includes(ai) );
    

    Unlike of it: Must contains all values.

    let allFounded = arr2.every( ai => arr1.includes(ai) );
    

    Hope, will be helpful.

    0 讨论(0)
提交回复
热议问题