0

I have several arrays that I json_encode form php and two variables that I get from an inputs, lets say :

let a = [1,2,3,4,5];
let b = [1,2,3,4,5];
let c = [1,2,3,4,5];
let d = [1,2,3,4,5];
let e = [1,2,3,4,5];
let f = [1,2,3,4,5];

let name = document.getElementById("some_element").value
let number = document.getElementById("some_other_element").value

I wanna check if the name is the same as the array name and then use the array with the name for example a[number].

I am not quite sure of how to do this. Any help would be really welcome.

JohnnyD
  • 403
  • 4
  • 16
  • So `#some_element`'s value might be `c` and `#some_other_element`'s value might be `4` and you'd want `c[4]` which is the number `5`? – D M Oct 26 '21 at 17:31
  • yeah sorry @DM it's like that, and the answer is what enzo wrote. – JohnnyD Oct 26 '21 at 17:44

1 Answers1

2

Use an object:

const arrays = {
  a: [1, 2, 3, 4, 5],
  b: [1, 2, 3, 4, 5],
  c: [1, 2, 3, 4, 5],
  d: [1, 2, 3, 4, 5],
  e: [1, 2, 3, 4, 5],
  f: [1, 2, 3, 4, 5],
};

const name = "a";
const number = 1;

console.log(arrays[name][number]);
enzo
  • 9,861
  • 3
  • 15
  • 38