How to Read Javascript Dictionary Key Value

Most of the fourth dimension, the databases are used for storing a big amount of data for using it afterward on or for saving the state of the application just there comes a demand to shop the data locally inside the code to use information technology subsequently. Therefore, programming languages have some standard data types which are really helpful in achieving such functionality without using the database.

Need of fundamental-value pairs

So for this purpose, JavaScript uses arrays and Objects to store some records within the lawmaking. Unlike other programming languages, JavaScript has no exact blazon to be called a Lexicon, but it does have a diverse type which is called Object in JavaScript. JavaScript's Object is quite flexible as you lot can use it dynamically wherever you lot want. Nearly of the time objects are widely used for the data as they tin can be easily utilized subsequently in the code.

Object

Objects are also the same as Dictionaries in other programming languages as the Objects also consist of the key Value pairs and the dictionaries are also based on this fundamental-value pair formation construction. Y'all can assign a primal against a specific value to access it subsequently on.

In this commodity, we are going to see how we tin can create dictionaries in JavaScript and how we tin easily manipulate some data into them with the help of some examples.

How to create a Lexicon/Object in JavaScript

Considering Dictionaries are non the traditional information types of JavaScript hence nosotros use them the same mode to create the dictionaries as we declare the objects and initialize some values to them.
Let's take an instance for creating the object.

Case

permit myFirstObject = new Object ( ) ;

// or the shorthand way
let myFirstObject = {
firstName: 'Mark' ,
lastName: 'Fonso' ,
age: twenty
}

console.log ( 'the type of the variable is :' , typeof (myFirstObject) , 'and its values are :' , myFirstObject)

There are 2 basic ways through which yous can create an object in JavaScript, the get-go ane is past declaring the name of the object which in our instance is "myFirstObject" and and then creating its instance by initializing a new Object().

The second way is considered as a shorthand method which is to simply declare the name of the object and assign it some key-value pairs to brand the interpreter understand that yes it is an object.

Output:

Hither you tin see that the blazon of object is Object as I said that there is no traditional data type chosen object hence JavaScript objects are treated equally objects in some cases.

Permit's take another example of creating a new Object in below example

Example

let expObject = {
firstName : "John Doe" ,
"10" : 10 ,
10 : "Any Integer" ,
"feel" : 0
} ;

console.log ( 'Type: ' , typeof (expObject) , '\northward' , 'Values: ' , expObject)

Here in the above instance, we take created and initialized a new object with the proper noun expObject and panel logged information technology into the terminal while checking its information blazon and displaying the values inside information technology.

Equally you can meet in the above output screen that the type is existence displayed as an Object while its values are too being displayed in the output final as we expected.

Assigning Values to the object keys

Nosotros take successfully created our object, at present let'south check out the values by using the fundamental names in the beneath example;

This may be accomplished past utilizing the Object's Indexer property to set the values, or by using it directly as if information technology were a normal holding on the object. The Primal values can be any object, non just strings:

permit expObject = {
firstName : "John Doe" ,
"Ten" : ten ,
x : "Any Integer" ,
"experience" : 0
} ;

expObject[ "10" ] = 20 ;
expObject[ ten ] = "Xx" ;

expObject[ "feel" ] = 22 ;

console.log ( 'Blazon: ' , typeof (expObject) , '\n' , 'Values: ' , ExpObject)

// Assigning property direct
dict.firstName = "John Doe" ;

Here in the in a higher place example, we have created an object and afterwards on, we take assigned the values exclusively to the specific properties or keys of the object by using the indexer method.

Output

You can at present clearly see in the above provided output screen that the values have been updated in the object.

Assigning values to the object using the dot operator (Straight Assignment)

Till now we take discussed how we can create objects and how we can assign some values to the backdrop of the object using the indexer method, here in the below instance we will see how nosotros can use a dot operator to assign some values to the properties of the object.

Example

permit expObject = {
firstName : "John Doe" ,
"Ten" : 10 ,
10 : "Whatsoever Integer" ,
"feel" : 0
} ;

expObject.Ten = xx ;
expObject.experience = 10 ;

expObject.firstName = 'Mark Huggy' ;

console.log ( 'Blazon: ' , typeof (expObject ) , '\n' , 'Values: ' , expObject )

Hither in the above example we are creating a object and later on on nosotros are assigning some values to its properties using dot operator or y'all can say a direct assignment in other words.

You can see that the higher up-highlighted values are updated in the object.

Iterating key-value pairs of the object

Now we volition discuss well-nigh iteration of the key-value pairs.

Example
Here in the post-obit example, we will create an object so nosotros will loop through each key of the object and display the value confronting each key of the object:

allow expObject = {
firstName : "John Doe" ,
"Ten" : 10 ,
10 : "Any Integer" ,
"experience" : 0
} ;
for (permit key in expObject ) {
permit value = expObject [fundamental] ;
panel.log (value) ;

}

Output

The values are being displayed against each fundamental with the help of a for loop

Directly Access to Central/Value Pairs

Same as straight Assignment, We can also directly access the values of the object using a dot operator and assign information technology to another variable for after usage. Let'southward see in the below example:

 let expObject= {
firstName : "John Doe" ,
"Ten" : 10 ,
10 : "Whatsoever Integer" ,
"experience" : 0
} ;

let first_name = expObject.firstName ;

expObject.firstName = "Mark Christonson" ;

panel.log ( 'Old name :' ,first_name, '\nNew Name: ' , expObject.firstName )

Here in the above example, we have created an object and afterwards, nosotros accept assigned its old firstName value to the first_name variable and updated the value of the firstName by using the straight assignment method. Afterward on, we take displayed both the old and the new values in the screen

Output:

Y'all tin can see both the one-time and the new name fields in the higher up output screen.

Conclusion

Dictionary is non the traditional data blazon of the JavaScript programming language, but the objects tin exist treated as dictionaries in some cases. Y'all can create an object through two different means, i is to declare it and initialize it using the new Object() method and the second one is the shorthand style by but declaring and assigning some cardinal-value pairs to it. In this article, we went through both methods in detail and we have discussed how we can assign and retrieve values using direct assignment and indexer methods.

About the author

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine artistic and usability viewpoints resulting in world-class web applications. I take feel working with Vue, React & Node.js & currently working on article writing and video cosmos.

rofeonfunien1984.blogspot.com

Source: https://linuxhint.com/create-dictionary-javascript-key-value-pairs/

0 Response to "How to Read Javascript Dictionary Key Value"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel