Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Calculate the length of an associative array using JavaScript
Next article icon

What are Associative Arrays in JavaScript ?

Last Updated : 10 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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); 

Output
Keys 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.


Next Article
Calculate the length of an associative array using JavaScript

G

geekwriter2024
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Geeks Premier League
  • javascript-array
  • Geeks Premier League 2023

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. Syntax cosnt arr = new Array( length )
    4 min read
  • Dense and Sparse Array in JavaScript
    In JavaScript, an array is a data structure used for storing a collection of values. Each value in the collection is assigned a unique index number. Arrays in JavaScript can contain values of any data type, such as strings, numbers, objects, and even other arrays. In JavaScript, arrays can be of two
    7 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
  • How to remove Objects from Associative Array in JavaScript ?
    In this article, we are going to learn about removing Objects from Associative Array in Javascript, In JavaScript, you can remove objects from an associative array (also known as an object) using the following methods. Table of Content Using JavaScript delete operatorUsing JavaScript Array.filter()
    4 min read
  • How to Create Zip Array in JavaScript ?
    Working with arrays in JavaScript often requires merging or combining multiple arrays into a single structure. One common operation in array manipulation is known as "zipping," where corresponding elements from multiple arrays are paired together to create a new array. Example: Input: let a = [1, 2,
    3 min read
  • JavaScript- Arrays are Equal or Not
    These are the following approaches to compare two arrays in JavaScript: 1. Using the JSON.stringify() MethodJavaScript provides a function JSON.stringify() method in order to convert an object whether or array into a JSON string. By converting it into JSON strings, we can directly check if the strin
    4 min read
  • Interesting Facts about JavaScript Arrays
    Let us talk about some interesting facts about JavaScript Arrays that can make you an efficient programmer. Arrays are ObjectsJavaScript arrays are actually specialized objects, with indexed keys and special properties. They have a length property and are technically instances of the Array construct
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences