温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.muo.com/javascript-dictionaries-create-use/
点击访问原文链接

JavaScript Basics: How to Create and Use a Dictionary

JavaScript Basics: How to Create and Use a Dictionary Menu Sign in now Close PC & Mobile Submenu Windows2 Linux Android Apple Technical Submenu Tech Explained Security Software Submenu Productivity Internet Creative Screen Submenu Entertainment Streaming Home Submenu Smart Home Home News Sign in Newsletter Menu Follow Followed Like More Action Sign in now Productivity Android Smart TVs Networking Windows 11 Entertainment Close
JavaScript Basics: How to Create and Use a Dictionary Unsplash - no attribution
Source - https://unsplash.com/photos/k-rKfqSm4L4 By  Shay Lynn Khan Published Dec 20, 2022, 4:31 PM EST Shay loves learning new things through personal projects. Outside coding, Shay also loves gaming and playing the piano. Sign in to your MakeUseOf account

A dictionary is a data structure that you can use to store data in your application. You can store data using a key-value pair, which allows you to look up and retrieve a specific value.

Once you have stored data in a dictionary, you can complete other actions such as iterating over each item. You can also check if an item exists, or delete an item that is no longer needed.

How to Create a Dictionary Object

A dictionary is one of the many important data structures that you can use to store data. You can create dictionaries in C# and many other programming languages. You can also create an equivalent hashmap data structure in Java.

There is no "dictionary" keyword that you can use to create a dictionary object in JavaScript. However, you can create a dictionary using a generic object. Here's an example of how you can create an empty dictionary using the "Object" keyword:

let dictionary = new Object();

You can also create an empty dictionary using this shorthand syntax:

let emptyDictionary = {};

If you would like to initialize the dictionary with values, you can add each value in the format of "key : value".

Using the example below, you can create a string key called "Pidgey", and associate it with a value. The value is an object with properties for the pet's age, color, and gender.

let petDictionary = {
"Pidgey": { Age: 0.5, Color: "Gray", Gender: "Male" },
"Mocha": { Age: 0.5, Color: "Brown", Gender: "Female" },
};

Keys are not restricted to string data types. You can use other data types such as numbers or boolean values.

let wcDictionary = {
1: { Team: "Argentina" },
2: { Team: "France" },
};

let dictBool = {
true: { Message: "Confirmed" },
false: { Message: "Denied" },
};
How to Add Values to the Dictionary Object

You can add new items to a dictionary using this format:

dictionary[new_key] = new_value

The new_key can be any valid key value of your choice. This is the key you will use later on when you want to access that specific item in the dictionary. The new_value can be any object or value that you want to associate with the key.

This is an example of how you can add a new item to a dictionary using some example values:

petDictionary["Apples"] = { Age: 2, Color: "Green", Gender: "Male" };

Just like when initializing, you can also use other data types to represent the key:

wcDictionary[3] = { Team: "Morocco" };
How to Access Values Based on a Key

You can access a value from a dictionary using its key value:

let dictionaryValue = petDictionary["Mocha"];
console.log(dictionaryValue);

The value returned will contain the entire object or value stored for that key:

Screenshot by Sharlene Von Drehnen How to Iterate Over Each Item in the Dictionary

You can iterate over each item in the dictionary using the Object.keys() method. The Object.Keys() method returns an array that contains all the keys used in the dictionary:

console.log(Object.keys(petDictionary));

In your console, you should then see an array containing all the dictionary’s keys:

Screenshot by Sharlene Von Drehnen

You can use the list of keys to loop through each item in the dictionary, and retrieve the value for each key:

for (const key of Object.keys(petDictionary)) {
console.log(key + ": ");
console.log(petDictionary[key]);
};

With the following results in your console:

Screenshot by Sharlene Von Drehnen How to Check if an Item Exists in the Dictionary

You can check if a key exists in the dictionary using the "in" keyword:

let inDictionary = 'Mocha' in petDictionary; // returns true
let notInDictionary = 'a' in petDictionary; // returns false

You can also use the hasOwnProperty() method to check if an item exists:

let exists = petDictionary.hasOwnProperty('Mocha'); // returns true
let doesNotExist = petDictionary.hasOwnProperty('a'); // returns false
How to Delete a Value From the Dictionary

You can set an item to null to indicate it has no value:

petDictionary['Apples'] = null;

That item, however, will still be present in the dictionary. If you want to remove the item altogether, you can delete it using the "delete" keyword:

delete petDictionary['Apples'];
Storing Data Inside Dictionaries in JavaScript

JavaScript doesn’t have first-class support for dictionaries, but you can use a plain Object to store key/value pairs.

A dictionary is a very powerful data structure that you can use to store and access data using keys. A dictionary isn't the only place where you can store data, so you can explore other data structures that could better suit your use case.

Close
Recommended How To Build Data Structures With JavaScript ES6 Classes 5 Raspberry Pi alternatives that are cheaper and work just as well Microsoft's new Windows 11 update has a hidden performance booster — here's how to get it early This cloud workspace gives your laptop the GPU it never had Join Our Team Our Audience About Us Press & Events Media Coverage Contact Us Follow Us Advertising Careers Terms Privacy Policies MakeUseOf is part of the Valnet Publishing Group Copyright © 2026 Valnet Inc.

JavaScript Basics: How to Create and Use a Dictionary,AI智能索引,全网链接索引,智能导航,网页索引

    A map of key/value pairs is an incredibly useful data structure. JavaScript has native support for dictionaries via its Object type.