https://blog.kevinchisholm.com/javasc...
In this video, I’ll answer the question, “Does JavaScript support associative arrays?”
Some languages such as PHP allow for associative arrays. So you may be asking yourself how to create an associative array in JavaScript. And the answer is, you can’t. JavaScript does not allow for associative arrays.
What’s confusing is that in JavaScript, an array is an object. So, an object can have properties that are string-based. For example, down here I’m adding a city property to the days array, and a color property to the days array and a speed property to the days array, and this is all perfectly legal.
But these properties don’t participate in methods such as push, pop, shift, and unshift. And they don’t come up when you reference the length of the array. When you’re iterating over an array, it’s only the numeric-based properties that will participate in the array.
So, for example, up here I have this days array. It has the days Monday through Friday, and there are five elements in the array. So I loop through the elements of the array, using lodash, and I output each element, or each day, in the DOM. So that’s how I wind up with Monday through Friday.
And then, I append one more element to the list. I’m showing the length of days. So, days right here has five elements. And then I do this. But here, I’m adding string-based properties. These properties do not have numeric names, or keys. They have string keys. They’re perfectly valid, but they don’t participate in methods such as push, pop, shift, and unshift, and they don’t show up in the length of the array.
So now that I’ve added these three properties, some people might think the length of the array is eight, but it’s not; it’s five. So now when I iterate over the array again, you can see that once again I wind up with Monday through Friday, and the length is five. And that’s because these properties do not show up in the length. They’re string-based properties.
Down here, I add three more properties to the array. But these properties are numeric. They have numeric keys. So days 5 = london, days 6 = red, and days 7 = 500. And doing these three things would be like saying days push London, days push red, days push 500. It’s the same exact thing.
So now when I loop through the array you can see that the array has eight elements. And that’s because these properties are numeric. So now these properties will participate in methods such as shift, unshift, push, and pop, and they show up in the length.
So it’s really important to keep in mind that in JavaScript, there’s no such thing as an associative array. The closest thing you have is objects. But, an array, while it’s an object, can have properties and you can add properties to that object, they will not participate in the array. But when the properties of an array have numeric keys or numeric names, they do participate in methods such as shift, unshift, push, and pop, and they appear in the length of the array.
If you found this video helpful, you can subscribe to my channel by clicking the red "Subscribe" button right under the video. You can also take a look at my blog, where there are many articles and tutorials about web development.
Thanks very much for watching.