Creating Object map in node.js

Creating an Object map in node.js and how can we access the static and dynamic objects.

Suppose we have a Object Map like below:

var myBooks = {
"fiction":{
copies:200,
cost:"$1000"
},
"non-fiction":{
copies:100,
cost:"$1200"
},
"fantasy":{
copies:300,
cost:"$2000"
},
}

When you run above code, it will print below output i.e array of the objects.

x1

 

So here, using dot(.) operator we can access the array of objects.

console.log(myBooks.fiction);
console.log(myBooks.fiction.copies);

The above code will print the following output:

{ copies: 200, cost: ‘$6,000’ }  //first console.log
200 //second console.log

But what if i do not know the object i have to access?

Here is how we can get the object values then,


var choice = "fantasy";

console.log(myBooks[choice].copies);

This will produce output as 300

 

Advertisements

2 thoughts on “Creating Object map in node.js

  1. Nice blog right here! Also your web site loads up very fast! What host are you the usage of? Can I am getting your associate hyperlink for your host? I desire my web site loaded up as quickly as yours lol

    Like

  2. Valuable info. Fortunate me I discovered your site by chance, and I am stunned why this coincidence did not happened in advance! I bookmarked it.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s