JavaScript Dictionary
Posted by Superadmin on May 02 2023 14:14:08

JavaScript Dictionary

JavaScript Dictionary

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?

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:

JavaScript Dictionary 1

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:

JavaScript Dictionary 2

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:

The author name is raman

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.