tldr; There are no associative arrays in Javascript. But there is indeed a credible alternative.
In PHP, we can do the following -
|
|
Javascript does not have associative arrays.
All you can do is -
|
|
Yes, you can define props for the array itself, but that is an altogether different thing.
|
|
The above statement does not “modify the array elements”. It just adds a prop to the array object. So, you cannot iterate and the length of array stays the same as before.
The alternative for associative arrays is just an object.
|
|
You do not have access to all the array methods, but could apply powerful iterations for objects through methods like Object.values.forEach()
.
You can observe the similarities between the PHP and Javascript way of doing things in the below code-
|
|