Updated April 1, 2023
Introduction to JavaScript Dictionary
In JavaScript, a dictionary is one of the datatypes; it can be used for the different sequences in the script and also, it does not contain the some flexible object. Because the object is the root cause of the, all the Object-Oriented Programming languages here JavaScript is one of the dynamically typed languages it does not provide with any datas to the dictionary using data structures in native nature. Collection of datas join together with proper structure is called a data structure dictionary. It can be implemented using the JavaScript object data type definition.
Syntax:
We already know that JavaScript is one of the object-oriented programming models. Therefore, each variable, datatypes and functions, arguments, etc., have its own syntax and format for achieving the goal in web-based projects.
<html>
<body>
<script>
var dictionary={""};
var dictionary= new Object();
---some javascript logics—
</script>
</body>
</html>
The above codes are the basic syntax for dictionary objects, and also we have created a separate instance of the dictionary in the script.
How is JavaScript Dictionary Done?
- The dictionary is one of the JavaScript collection packages it’s mainly denoted and declared the values as key-value pairs its come under the Map interface the keys are required to be the string format using this constraint it may have to place the mapping for simplifying the assumptions and used with the other features like plain JavaScript objects it has been allotted the memory in the backend stores. So we have to pass the arguments in the dictionary object before that; we must specify the constructor that is used to create the instance in the initial stage.
- In the initial stage, the dictionary creates an object, and it provides one key with one value both has to be matched and mapped together in the data variable. We know that JavaScript does not provide any dictionary data structure in native nature that data structure will be implemented with the help of a parent class called object datatype. A dictionary consists of single or multiple value items that have to be used for just key and value pairs. Whether the key is used to select, search and filter the item, value is assigned and used for storing some data inside with the item. We will create the object in the script in different ways, especially in dictionary data type; we can use it in 3 different ways.1. Creating an object using a new keyword like var v=new objet().2. We can create the object using curly braces({}) like var v={} its also creating a separate instance and memory address in the heap memory.3.
- The final way is we have to create the dictionary object by setting some data items like key/value pairs var v={key:value} whether key and value use any data types like strings, integer etc. We can populate the dictionary items using key-value pairs create the item using dot operator notation in this scenario named it as add or populate the dictionary. Then, we will remove the item in the dictionary using the delete keyword in the javascript, where we will provide the data items using dot or square format like delete dictionary. Ismail and delete dictionary[12]; these are the syntax codes used to remove the item in the dictionary type whenever we remove the item in a dictionary with the help of a key. Generally, the dictionary will used multiple keys for iterating the loops over them.
- Where we can use the JavaScript in some loops like for, forEach, while etc., mainly for and forEach is most widely used for iterating the values in the keyscript with the help of keys. We already mentioned it as with the help of keys, and we can retrieve the items using the symbols or special characters like square parenthesis and dot usages. We will change and update the values using key mainly we will update the existing item values; it will not be deleted; it just changes or replaces the existing item with a new item. Then, using some conditional statements like if-else, we will validate the keys and values in the script.
Examples of JavaScript Dictionary
Given below are the examples of JavaScript Dictionary:
Example #1
Code:
<!DOCTYPE HTML>
<html>
<body>
<script>
function dictionary() {
this.a = new Array();
this.b = b;
this.a = new Array();
this.c = c;
this.d = d;
this.e = e;
this.f = f;
this.g = g;
this.h = h;
}
function b(k, v) {
this.a[k] = v;
}
function c(k) {
return this.a[k];
}
function d(k) {
delete this.a[k];
}
function e() {
for (var k in Object.keys(this.a).sort()) {
document.write(k + " -> " + this.a[k]);
}
}
function f() {
for (var k in Object.keys(this.a).sort()) {
document.write(k + " -> " + this.a[k]);
}
}
function g() {
var s = 0;
for (var k in Object.keys(this.a)) {
++s;
}
return s;
}
function h() {
for (var k in Object.keys(this.a)) {
delete this.a[k];
}
}
var book = new Dictionary();
book.b("siva","098");
book.b("raman", "765");
book.b("arun", "432");
document.write("Key is to be finding using: " + book.c("raman"));
book.d("B");
book.e();
book.f();
book.h();
documnet.write("Number of key and value counts: " + book.g());
</script>
</body>
</html>
Output:
Example #2
Code:
<!DOCTYPE HTML>
<html>
<body>
<script>
var dictionary = { 1: 'ID', raman: 'name',tup: 'city' };
document.write(Object.keys(dictionary));
var dictionary1 = { 2: 'ID', siva: 'name',dq: 'city' };
document.write(Object.keys(dictionary1));
var dictionary2 = { 3: 'ID', arun: 'name',qw: 'city' };
document.write(Object.keys(dictionary2));
var dictionary3 = {4: 'ID', kumar: 'name',tup: 'city' };
document.write(Object.keys(dictionary3));
var dictionary4 = {5: 'ID', fergr: 'name',tup: 'city' };
document.write(Object.keys(dictionary4));
var dictionary5 = {6: 'ID', rfeg: 'name',tup: 'city' };
document.write(Object.keys(dictionary5));
var dictionary6 = {7: 'ID', eff: 'name',tup: 'city' };
document.write(Object.keys(dictionary6));
var dictionary7 = {8: 'ID', er: 'name',tup: 'city' };
document.write(Object.keys(dictionary7));
var dictionary8 = {9: 'ID',gebj: 'name',tup: 'city' };
document.write(Object.keys(dictionary8));
</script>
</body>
</html>
Output:
Example #3
Code:
<!DOCTYPE HTML>
<html>
<body>
<p id="demo"></p>
<script>
var dictionary = {language:"english", book:"siva", author:"raman"};
document.getElementById("demo").innerHTML = "The author name is " + dictionary.author;
</script>
</body>
</html>
Output:
Conclusion
In JavaScript dictionary, we have used for storing and retrieving the values in the script or backend db if needed to retrieve or store it in the db or memory areas and quickly perform the finding operations in JavaScript.
Recommended Articles
This is a guide to JavaScript Dictionary. Here we discuss the introduction; how is JavaScript dictionary done? And examples, respectively. You may also have a look at the following articles to learn more –