简体   繁体   中英

Find Array[1D] in Array[2D] and return index

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM