You can create an associative array in JavaScript using an array of objects with key and value pair.
Associative arrays are basically objects in JavaScript where indexes are replaced by user defined keys.
Example
var customerDetails= [ { "customerId":"customer-1", "customerName":"David", "customerCountryName":"US" }, { "customerId":"customer-2", "customerName":"Bob", "customerCountryName":"UK" }, { "customerId":"customer-3", "customerName":"Carol", "customerCountryName":"AUS" } ] for(var i=0;i<customerDetails.length;i++){ console.log(customerDetails[i].customerName); }
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo115.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo115.js David Bob Carol