
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Creating an associative array in JavaScript?
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
- Related Questions & Answers
- Creating an associative array in JavaScript with push()?
- Dynamically creating keys in JavaScript associative array
- JavaScript in filter an associative array with another array
- Sorting an associative array in ascending order - JavaScript
- Length of a JavaScript associative array?
- JavaScript creating an array from JSON data?
- PHP array_push() to create an associative array?
- PHP Pushing values into an associative array?
- PHP Associative Array
- Convert an object to associative array in PHP
- How to use associative array/hashing in JavaScript?
- How to generate array key using array index – JavaScript associative array?
- Creating an array of objects based on another array of objects JavaScript
- How to access an associative array by integer index in PHP?
- Grouping an Array and Counting items creating new array based on Groups in JavaScript
Advertisements