简体   繁体   English

使用字符串搜索在多维数组中查找坐标

[英]Find a coordinate in a multidimensional array using string search

Try to give an alternative in this question WITHOUT LOOPING! 尝试在这个问题上给一个选择,不要让步! Just using indexOf and some integer math 仅使用indexOf和一些整数数学

Get coordinates of an element in multidimentional array in Javascript 在Javascript中获取多维数组中元素的坐标

The code below seemed promising but fails. 下面的代码似乎很有希望,但失败了。

Anyone with better math skills feel like fixing it? 任何具有更高数学技能的人都想解决它吗?

 var letterVariations = [ [' ','0','1','2','3','4','5','6','7','8','9'], ['A','a','B','b','C','c','D','d','E','e',';'], ['Â','â','F','f','G','g','H','h','Ê','ê',':'], ['À','à','I','i','J','j','K','k','È','è','.'], ['L','l','Î','î','M','m','N','n','É','é','?'], ['O','o','Ï','ï','P','p','Q','q','R','r','!'], ['Ô','ô','S','s','T','t','U','u','V','v','“'], ['W','w','X','x','Y','y','Ù','ù','Z','z','”'], ['@','&','#','[','(','/',')',']','+','=','-'], ]; var string = JSON.stringify(letterVariations); var pos = string.indexOf("u") console.log(Math.floor((pos/10)%8),pos%10) // fails, how to fix? pos = string.indexOf("M") console.log(Math.floor((pos/10)%8),pos%10) 

 function findPos(array, symbol) { const string = array.toString().replace(/,/g, ''); const pos = string.indexOf(symbol) const d = (array[0] || []).length const x = pos % d; const y = Math.floor(pos / d) return { x, y } } const array = [ [' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', ';'], ['Â', 'â', 'F', 'f', 'G', 'g', 'H', 'h', 'Ê', 'ê', ':'], ['À', 'à', 'I', 'i', 'J', 'j', 'K', 'k', 'È', 'è', '.'], ['L', 'l', 'Î', 'î', 'M', 'm', 'N', 'n', 'É', 'é', '?'], ['O', 'o', 'Ï', 'ï', 'P', 'p', 'Q', 'q', 'R', 'r', '!'], ['Ô', 'ô', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', '“'], ['W', 'w', 'X', 'x', 'Y', 'y', 'Ù', 'ù', 'Z', 'z', '”'], ['@', '&', '#', '[', '(', '/', ')', ']', '+', '=', '-'], ]; console.log(findPos(array,' ')) //=> [0, 0] console.log(findPos(array,'M')) //=> [4, 4] console.log(findPos(array,'u')) //=> [6, 7] console.log(findPos(array,'-')) //=> [8, 10] 

You could join the strings and use the length of the inner array as value for divisioin or for the remainder operator. 您可以连接字符串,并将内部数组的长度用作除数或余数运算符的值。 This works only for strings with a single character. 这仅适用于具有单个字符的字符串。

 var letterVariations = [ [' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', ';'], ['Â', 'â', 'F', 'f', 'G', 'g', 'H', 'h', 'Ê', 'ê', ':'], ['À', 'à', 'I', 'i', 'J', 'j', 'K', 'k', 'È', 'è', '.'], ['L', 'l', 'Î', 'î', 'M', 'm', 'N', 'n', 'É', 'é', '?'], ['O', 'o', 'Ï', 'ï', 'P', 'p', 'Q', 'q', 'R', 'r', '!'], ['Ô', 'ô', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', '“'], ['W', 'w', 'X', 'x', 'Y', 'y', 'Ù', 'ù', 'Z', 'z', '”'], ['@', '&', '#', '[', '(', '/', ')', ']', '+', '=', '-'] ], string = letterVariations.map(a => a.join('')).join(''), pos = string.indexOf("u"); console.log(Math.floor(pos / 11), pos % 11); pos = string.indexOf("M") console.log(Math.floor(pos / 11), pos % 11); 

This produces the correct result. 这将产生正确的结果。 There is no need to stringify, you can flatten the arrays and use indexOf to get the position: 无需进行字符串化,您可以展平数组并使用indexOf获取位置:

 var letterVariations = [ [' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], ['A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', ';'], ['Â', 'â', 'F', 'f', 'G', 'g', 'H', 'h', 'Ê', 'ê', ':'], ['À', 'à', 'I', 'i', 'J', 'j', 'K', 'k', 'È', 'è', '.'], ['L', 'l', 'Î', 'î', 'M', 'm', 'N', 'n', 'É', 'é', '?'], ['O', 'o', 'Ï', 'ï', 'P', 'p', 'Q', 'q', 'R', 'r', '!'], ['Ô', 'ô', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', '“'], ['W', 'w', 'X', 'x', 'Y', 'y', 'Ù', 'ù', 'Z', 'z', '”'], ['@', '&', '#', '[', '(', '/', ')', ']', '+', '=', '-'], ]; var flattened = letterVariations.flat() var findLetter = function(letter) { var pos = flattened.indexOf(letter), x = Math.floor((pos / 10) % 8), y = (pos - (pos % 11)) / 11; return { letter: letter, x: x, y: y } } console.log(findLetter(' ')) //=> [0, 0] console.log(findLetter('M')) //=> [4, 4] console.log(findLetter('u')) //=> [6, 7] console.log(findLetter('-')) //=> [8, 10] 

Here is one version of that: 这是一个版本:

 var letterVariations = [ [' ','0','1','2','3','4','5','6','7','8','9'], ['A','a','B','b','C','c','D','d','E','e',';'], ['Â','â','F','f','G','g','H','h','Ê','ê',':'], ['À','à','I','i','J','j','K','k','È','è','.'], ['L','l','Î','î','M','m','N','n','É','é','?'], ['O','o','Ï','ï','P','p','Q','q','R','r','!'], ['Ô','ô','S','s','T','t','U','u','V','v','“'], ['W','w','X','x','Y','y','Ù','ù','Z','z','”'], ['@','&','#','[','(','/',')',']','+','=','-'], ]; const findLetterIn = letterVariations => { const width = letterVariations[0].length * 4 + 2; const alpha = JSON.stringify(letterVariations) return (char, pos = alpha.indexOf(char)) => pos > -1 ? [Math.floor((pos - 1) / width), (((pos - 1) % width) - 2)/4] : [-1, -1] } const findLetter = findLetterIn (letterVariations) console.log(findLetter(' ')) //=> [0, 0] console.log(findLetter('M')) //=> [4, 4] console.log(findLetter('u')) //=> [6, 7] console.log(findLetter('-')) //=> [8, 10] 

Here width has to do with row width. 这里的width与行的宽度有关。

The 4 s have to do with u ~> "u", The + 2 has to do with adding [ and ] to the beginning and end (as well as an additional , after the ] , but removing one before it.) The - 1 has to do with ignoring the initial [ and the - 2 has to do with removing the leading ," or, for the first one, the leading [" . 4 ■找做u ~> "u",+ 2具有与添加做[]的开始和结束(以及附加的,] ,但除去一个之前。)的- 1与忽略开头[ ,而- 2与删除前导," ,有关,对于第一个,则与前导["

You can switch to 1-based indices by adding 1 to both element of the returned array. 您可以通过将1添加到返回数组的两个元素中来切换到基于1的索引。

Based on @GluePear answer 基于@GluePear的答案

You can even use multi-chars in this solution 您甚至可以在此解决方案中使用多字符

 function findPos(array, symbol) { const string = array.flat(); const pos = string.indexOf(symbol) const d = (array[0] || []).length const x = pos % d; const y = Math.floor(pos / d) return { x, y } } const array = [ [' ','0','1','2','3','4','5','6','7','8','9'], ['A','a','B','b','C','c','D','d','E','e',';'], ['Â','â','F','f','G','g','H','h','Ê','ê',':'], ['À','à','I','i','J','j','K','k','È','è','.'], ['L','l','Î','î','M','m','N','n','É','é','?'], ['O','o','Ï','ï','P','p','Q','q','R','r','!'], ['Ô','ô','S','s','T','t','U','u','V','v','“'], ['W','w','X','x','Y','y','Ù','ù','Z','z','”'], ['@','&','#','[','(','/',')',']','+','=','-'], ]; console.log(findPos(array, '-')) 

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

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