Understanding array when it has name instead of index

Hi,
I am trying to understand a line of code that I saw e.g this.property = propertyItem[this.language]
I am aware of format such as this.property = propertyItem[2] where what we pass into [] is a number/
Can someone explain how array work if a variable name is passed into it

What’s stored inside of the variable this.language?

Nothing, it has type which is set to String, but I have seen such pattern elsewhere and it spin my head

Absolutely nothing is inside of this.language? That’s odd.

If this propertyItem is an array, then any key should be a number or a string representation of a number. If this is an object, this any key should be a string.

Can u mimic a example for me as I have hard time visualizing without number indices if it is not much a hassle?


const someIndex = 2;
const myArray = [1, 2, 3, 4, 5];
console.log(myArray[someIndex]);
1 Like

Thanks, so it is pretty much pointing to a number which can be indecies but why do it like that, is there any advantage over myArray[2]

myArray[2] must always and forever be the second element of myArray. But often we want to write logic that works the same way no matter which element of the array we access.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.