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.

2
  • 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? Commented Oct 26, 2021 at 17:31
  • yeah sorry @DM it's like that, and the answer is what enzo wrote. Commented Oct 26, 2021 at 17:44

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

yes thanks mate I didn't think of that thank you very much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.