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.
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
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
LikeLike
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.
LikeLike