Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an associative array with integer keys

I need to create an associative array in javascript with an integer key as follows;

a["10"] = "ten";

but when i create an array, it puts the value to the 10th index of the array and it creates an array with the length 11. I want it to be a key value pair. I know this can be done by using objects but i need an array only.

like image 497
Vishal Arora Avatar asked Jul 15 '26 10:07

Vishal Arora


1 Answers

JavaScript does not have associative arrays. The only way to do this in JavaScript is to use objects:

var a = {
    '10': 'ten'
};
like image 179
Joseph Silber Avatar answered Jul 21 '26 00:07

Joseph Silber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!