How to check if an array contains another array?

后端 未结 6 1736
后悔当初
后悔当初 2020-11-28 16:36

I needed 2d arrays, so I made nested array since JavaScript doesn\'t allow them.

They look like that:

var myArray = [
      [1, 0],
      [1, 1],
            


        
相关标签:
6条回答
  • 2020-11-28 16:55

    For those who are interested in finding an array inside another and get back an index number, here's a modified version of mohamed-ibrahim's answer:

    function findArrayInArray(innerArray, outerArray) {
        const innerArrayString = JSON.stringify(innerArray);
        let index = 0;
        const inArray = outerArray.some(function (element) {
            index ++;
            return JSON.stringify(element) === innerArrayString;
        });
        if (inArray) {
            return index - 1;
        } else {
            return -1;
        }
    }
    findArrayInArray([1, 2, 3], [[3, .3], [1, 2, 3], [2]]); // 1
    findArrayInArray([1, 2, 3], [[[1], 2, 3], [2]]) // -1
    

    This function returns the index of the array you are searching inside the outer array and -1 if not found.

    Checkout this CodePen.

    0 讨论(0)
  • 2020-11-28 17:03

    The code provided by D. Young's comment that checks for any length array is faulty. It only checks if the first element is the same.

    A corrected version of D. Young's comment:

    function isArrayInArray(source, search) {
        var searchLen = search.length;
        for (var i = 0, len = source.length; i < len; i++) {
            // skip not same length
            if (source[i].length != searchLen) continue;
            // compare each element
            for (var j = 0; j < searchLen; j++) {
                // if a pair doesn't match skip forwards
                if (source[i][j] !== search[j]) {
                    break;
                } else if (j == searchLen - 1) {return true}
            }
        }
        return false; 
    }
    
    0 讨论(0)
  • 2020-11-28 17:05

    Short and easy, stringify the array and compare as strings

    function isArrayInArray(arr, item){
      var item_as_string = JSON.stringify(item);
    
      var contains = arr.some(function(ele){
        return JSON.stringify(ele) === item_as_string;
      });
      return contains;
    }
    
    var myArray = [
      [1, 0],
      [1, 1],
      [1, 3],
      [2, 4]
    ]
    var item = [1, 0]
    
    console.log(isArrayInArray(myArray, item));  // Print true if found
    

    check some documentation here

    0 讨论(0)
  • 2020-11-28 17:05

    You can't do like that .instance you have to do some thing by your own .. first you have to do a foreach from your array that you want to search and run 'compareArray' function for each item of your array .

    function compareArray( arrA, arrB ){
    
        //check if lengths are different
        if(arrA.length !== arrB.length) return false;
    
    
        for(var i=0;i<arrA.length;i++){
             if(arrA[i]!==arrB[i]) return false;
        }
    
        return true;
    
    }
    
    0 讨论(0)
  • 2020-11-28 17:10

    A nested array is essentially a 2D array, var x = [[1,2],[3,4]] would be a 2D array since I reference it with 2 index's, eg x[0][1] would be 2.

    Onto your question you could use a plain loop to tell if they're included since this isn't supported for complex arrays:

    var x = [[1,2],[3,4]];
    var check = [1,2];
    function isArrayInArray(source, search) {
        for (var i = 0, len = source.length; i < len; i++) {
            if (source[i][0] === search[0] && source[i][1] === search[1]) {
                return true;
            }
        }
        return false;
    }
    console.log(isArrayInArray(x, check)); // prints true
    

    Update that accounts for any length array

    function isArrayInArray(source, search) {
        var searchLen = search.length;
        for (var i = 0, len = source.length; i < len; i++) {
            // skip not same length
            if (source[i].length != searchLen) continue;
            // compare each element
            for (var j = 0; j < searchLen; j++) {
                // if a pair doesn't match skip forwards
                if (source[i][j] !== search[j]) {
                    break;
                }
                return true;
            }
        }
        return false;
    }
    console.log(isArrayInArray([[1,2,3],[3,4,5]], [1,2,3])); // true
    
    0 讨论(0)
  • 2020-11-28 17:15

    Here is an ES6 solution:

    myArray.some(
        r => r.length == itemTrue.length &&
             r.every((value, index) => itemTrue[index] == value)
    );
    

    Check the JSFiddle.

    Take a look at arrow functions and the methods some and every of the Array object.

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