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))
3
  • 1.What is the expected output? 2.Do you want to search number 1 or array [1,2,3,4,5]? Commented Feb 24, 2020 at 9:05
  • Does this answer your question? Javascript 2d array indexOf Commented Feb 24, 2020 at 10:52
  • thankful for all helping , many thx. Commented Feb 24, 2020 at 11:11

1 Answer 1

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));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.