0

have any way can find a match index in array[2D] faster? I know compare 1 by 1 can make this ok, but i don't wanted to.

I tried this one, but it only can return -1

// mainsheetvalues is array[1D],
[1,2,3,4,5]


// AsheetValues is array [2D]
[
  [1,2,3,4,5],
  [6,7,8,9,0]
]

Logger.log(mainsheetvalues.indexOf(AsheetValues))
TheMaster
  • 45,448
  • 6
  • 62
  • 85

1 Answers1

0

As per this answer, we cannot compare two arrays directly. You need to generate some custom logic for the comparison. I have added a logic below. Hope this helps.

const AsheetValues  = [
    [1,2,3,4,5],
    [6,7,8,9,0]
]   
const mainsheetvalues  = [1,2,3,4,5];
const isIncluded = (parentArr, childArr) => {
    let isMatch = true;
    for(let parentLoopIndex = 0; parentLoopIndex < parentArr.length; parentLoopIndex++) {
        if (parentArr[parentLoopIndex].length != childArr.length)
            isMatch = false;
        
            for (var i = 0; i < parentArr[parentLoopIndex].length; i++) {
                if (parentArr[parentLoopIndex][i] != childArr[i]) { 
                    isMatch = false;   
                }           
            }   
            if (isMatch)  {
                parentLoopIndex = parentArr.length;
            }    
        }
    return isMatch;
}

console.log(isIncluded(AsheetValues, mainsheetvalues));
Nitheesh
  • 19,238
  • 3
  • 22
  • 49