1

I want to where is placed an element in an multidimentional array like 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','”'],
  ['@','&','#','[','(','/',')',']','+','=','-'],
 ];
 var coordinates = letterVariations.indexOf('u');
 console.log(coordinates);
 // Want to know that 'u' is 7 in the 7th array

Is it possible ?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • for each **row** in the outer array you would/could use **findIndex** to locate the item in question. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex – gkelly Mar 13 '19 at 15:04
  • `var char = "@", x, y = letterVariations.findIndex(row => (x=row.indexOf(char))!==-1), coordinates = y!==-1? [y,x]: null;` – Thomas Mar 14 '19 at 05:48
  • This one is cool. https://stackoverflow.com/a/55146278/295783 – mplungjan Mar 14 '19 at 06:51

2 Answers2

1

Run a simple for loop and check if the character exists in the inner array using indexOf. return immediately once a match is found

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', '”'],
  ['@', '&', '#', '[', '(', '/', ')', ']', '+', '=', '-'],
];

function getCoordinates(array, char) {
  for (let i = 0; i < array.length; i++) {
    const i2 = array[i].indexOf(char);
    if (i2 !== -1)
      return [i, i2]
  }
  return undefined
}

console.log(getCoordinates(letterVariations, 'u'))
console.log(getCoordinates(letterVariations, '@'))

Note: This returns the indexes and it is zero-based. If you want [7, 7], you need to return [i+1, i2]

Joe Warner
  • 3,335
  • 1
  • 14
  • 41
adiga
  • 34,372
  • 9
  • 61
  • 83
  • @JoeWarner please don't make such trivial edits. Especially when changing `let` to `const` makes little difference to the quality of this answer. – adiga Mar 15 '19 at 11:07
  • is it not best practice to use const for immutable properties? using let here has no benefit\. apologies if its a style thing feel free to change back... – Joe Warner Mar 15 '19 at 11:08
0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

const 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','”'],
  ['@','&','#','[','(','/',')',']','+','=','-'],
];

function getIndexOfLetter(letter) {
  return letterVariations.reduce((result, values, index) => {
    if (result[0] > -1) return result;

    const found = values.indexOf(letter);

    return found > -1 ? [Number(index), found] : result;
  }, [-1, -1])
}

console.log(getIndexOfLetter('k'))
Joe Warner
  • 3,335
  • 1
  • 14
  • 41