What are Associative Arrays in JavaScript ?
Last Updated :
10 Jun, 2024
Associative arrays in JavaScript, commonly referred to as objects, are crucial for storing key-value pairs. This guide explores the concept and usage of associative arrays, providing insights into their benefits and applications.
Example:
// Creating an associative array (object)
let arr= {
name: "Geek",
age: 10,
city: "Noida"
};
// Accessing values using keys
console.log(person.name);
console.log(person.age);
console.log(person.city);
We will perform some operations on associative arrays now:
Example 1: In this example, we will Calculate the length of an associative array.
JavaScript
let arr = { "apple": 10, "grapes": 20 };
arr["guava"] = 30;
arr["banana"] = 40;
// Printing the array
// returned by keys() method
console.log(Object.keys(arr))
// printing the size
console.log("Size = " + Object.keys(arr).length)
Output[ 'apple', 'grapes', 'guava', 'banana' ]
Size = 4
Example 2: In this example, we will see how to Sort an Associative Array by its Values
JavaScript
let a = []; // Array
// Add elemets to it
a.push({ name: 'a', class: 1 });
a.push({ name: 'b', class: 9 });
a.push({ name: 'c', class: 2 });
a.push({ name: 'd', class: 6 });
// Custom sorting function
let sortedList = a.sort(function (a, b) {
return a.class - b.class;
});
// Output
console.log(sortedList);
Output[
{ name: 'a', class: 1 },
{ name: 'c', class: 2 },
{ name: 'd', class: 6 },
{ name: 'b', class: 9 }
]
Example 3: In this example, we will see how to list of associative array keys in JavaScript.
JavaScript
// Input Associative Array
let arr = {
Newton: "Gravity",
Albert: "Energy",
Edison: "Bulb",
Tesla: "AC",
};
// getting keys using getOwnPropertyNames
const keys = Object.getOwnPropertyNames(arr);
console.log("Keys are listed below ");
// Display output
console.log(keys);
OutputKeys are listed below
[ 'Newton', 'Albert', 'Edison', 'Tesla' ]
Common Use Cases:
While detailed examples will be discussed in the approaches section, here’s a brief overview:
- Dynamic Property Assignment: Adding or updating properties dynamically.
- Data Storage and Retrieval: Efficiently managing and accessing data with named keys.
Understanding associative arrays in JavaScript is essential for effective data management and manipulation. This guide introduces the importance and prerequisites, preparing you for deeper exploration of their usage. By focusing on practical applications, you can improve your JavaScript proficiency and code efficiency.
Similar Reads
How to Declare an Array in JavaScript? Array in JavaScript are used to store multiple values in a single variable. It can contain any type of data like - numbers, strings, booleans, objects, etc. There are varous ways to declare arrays in JavaScript, but the simplest and common is Array Litral Notations. Using Array Literal NotationThe b
3 min read
Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J
3 min read
Arrays in JavaScript behaves like an Object In this article, we will learn how you check that javascript arrays behave like objects and why it happens. We will be showing the key and values in the array whether you define the key or not in an array of JavaScript. Reason for Arrays behaving like an object: In JavaScript, there is no real Array
2 min read
Calculate the length of an associative array using JavaScript In JavaScript, we have normal arrays in which an element is present at a particular index. Whereas Associative arrays are basically Objects in JavaScript where the index is replaced with user-defined keys. Basically, we can say that it stores Key-Value pairs. Syntax: let arr = { key1: 'value'1, key2
4 min read
How to use Associative Array in TypeScript ? Typescript Associative arrays are objects in which indexes are replaced by user-defined keys. Associative Array can be used like a regular array, but the only difference is that it can be accessed using strings instead of numbers. Syntax:let associativeArray: { [key: string]: string } = { key1: "val
2 min read