简体   繁体   English

在 Array[2D] 中查找 Array[1D] 并返回索引

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

have any way can find a match index in array[2D] faster?有什么办法可以更快地在 array[2D] 中找到匹配索引? 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我试过这个,但它只能返回-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.根据这个答案,我们不能直接比较两个 arrays。 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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