Implement Associative Arrays In JavaScript Part II of Array Series
An array specifies a variable that can be indexed as a list in rows and columns. Associate array stores a collection of named key and value pairs.
The first index is zero as is common in most programming languages. JavaScript objects can be used as associative arrays.
An array is a data structure consisting of a collection of elements which are values or variables.
Common Functions For Manipulating Associative Arrays In JavaScript
Common Array Functions
Name | Description | Example |
---|---|---|
Object.assign() | Copies properties to target object from source objects | Object.assign(pickup_truck2, pickup_truck); |
Object.entries() | Generates array of key and value pairs | let pt = Object.entries(pickup_truck); |
Object.values() | Generates array of object values | let va = Object.values(pickup_truck); |
Name | Description | Example |
JavaScript Associative Arrays Snippet
// OjamboShop.com Learning JavaScript Arrays Part II Tutorial // let pickup_truck = {"brand":"Ford", "model":"F-Series", "year":1984}; console.log(pickup_truck["brand"]); console.log(pickup_truck.brand); console.log(pickup_truck.model); console.log(pickup_truck.year); let pickup_truck2 = []; pickup_truck2["brand"] = "Dodge"; pickup_truck2.push({"transmission": "manual"}); console.log(pickup_truck2.brand); console.log(pickup_truck2.transmission); Object.assign(pickup_truck2, pickup_truck); // Assign Source To Target console.log(pickup_truck2.brand); console.log(pickup_truck2.model); console.log(pickup_truck2.year); console.log(pickup_truck2.transmission); let pt = Object.entries(pickup_truck); // Generate Key Value Pair Array console.log(pt); let va = Object.values(pickup_truck); // Generate Values Array console.log(va);
Usage
You can use any IDE or text editor and the web browser to compile and execute JavaScript code. For this tutorial, the OjamboShop.com Learning JavaScript Course Web IDE was used to input and compile JavaScript code for the associative arrays.
Open Source
JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. This allows commercial use, modification, distribution, and allows making derivatives proprietary.
Learn Programming Courses:
Courses are optimized for your web browser on any device.
Limited Time Offer:
OjamboShop.com is offering 20% off coupon code SCHOOL for Learning JavaScript Course or Learning Python Course or for Learning PHP Course until End Day 2024.
Learn Programming Ebooks:
Ebooks can be downloaded to your reader of choice.
Conclusion:
JavaScript makes it easy to use associative arrays. Use associative arrays to for a collection of elements in key and value pairs.
Take this opportunity to learn the JavaScript, Python or PHP programming language by making a one-time purchase at Learning JavaScript Course or Learning Python Course or Learning PHP Course. A web browser is the only thing needed to learn JavaScript, JavaScript or PHP in 2024 at your leisure. All the developer tools are provided right in your web browser.
If you prefer to download ebook versions for your reader then you may purchase at Learning JavaScript Ebook or Learning Python Ebook or Learning PHP Ebook
For custom websites, app development and one-on-one tutorials, go to OjamboServices.com.
References:
- JavaScript Arrays Article Part I
- Learning JavaScript Course on OjamboShop.com
- Learning Python Course on OjamboShop.com
- Learning PHP Course on OjamboShop.com
- Learning JavaScript Ebook on Amazon
- Learning Python Ebook on Amazon
- Learning PHP Ebook on Amazon
- OjamboServices.com For Custom Websites, Applications & Tutorials