How to create map/array in javascript? 2013

View previous topic View next topic Go down

How to create map/array in javascript? 2013

Post by Admin on Sun Sep 15, 2013 4:02 am

2013 2013 2013 <div dir="ltr" style="text-align: left;" trbidi="on"><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"><br />var keyOne = "keyOne";<br />var keyTwo = "keyTwo";<br />var valueOne = "valueOne";<br />var valueTwo = "valueTwo";<br />// Map with two keys and two values<br />var map = {keyOne:valueOne,keyTwo:valueTwo}<br /><br />// Get function to get the value from key<br />function get(k){<br /> return map[k];<br />}<br /><br />// Create an array as shown below<br />var array = new Array(10);//It creates an empty array of length 10<br />array[0] = "Array Index 0";<br />array[1] = "Array Index 1";<br />array[2] = "Array Index 2";<br /><br />// Test Results<br />//alert("Value for key one in a map : "+get("keyOne"));<br />//alert("Value for key two in a map : "+get("keyTwo"));<br />//alert("Value from array : " + array[0]);<br /><br />var arrayWithElements = new Array(10,20,30); //It creates an with 3 elements<br />var arrayLiteral = [10,20,30]; //It creates an with 3 elements using the array literal<br /><br /></code></pre></br> In order to get the key and value from the map if you does not knows the key, then use following code. It may happen if there is a requirement to iterate through the map without having knowlege of keys that are stored in a map. In that case use the below code.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">for (var key in map) {<br /> //alert("Property \"" + key + "\" = " + map[key]);<br />}</code></pre> <!--Advertisment Starts--> <br /><table border="0" id="amazon_body_ads_com" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl-20&o=1&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B00DVFLJDS" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=B0075SUHWG&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=B006P88VSE&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=0316206849&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table> <table border="0" id="amazon_body_ads_in" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=8173666024&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=0006479901&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=1847370292&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=9350042096&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table> <table border="0" id="amazon_body_ads_uk" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B000C1ZDTU" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007VBGTX8" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B009SJCW3C" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B009VK73FA" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td></tr></tbody></table> <table border="0" id="amazon_body_ads_ca" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007HCCOD0" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007FXPWCQ" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007XZM6VG" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B00834SJS0" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table></br> <script>//<![CDATA[ var tokens = document.domain.split('.'); var domain = tokens[tokens.length - 2]; if(tokens[tokens.length - 1]=="uk") { document.getElementById("amazon_body_ads_uk").style.display=""; } else if(tokens[tokens.length - 1]=="com") { document.getElementById("amazon_body_ads_com").style.display=""; } else if(tokens[tokens.length - 1]=="in") { document.getElementById("amazon_body_ads_in").style.display=""; } else if(tokens[tokens.length - 1]=="ca") { document.getElementById("amazon_body_ads_ca").style.display=""; } else{ document.getElementById("amazon_body_ads_com").style.display=""; } //]]></script></div><br> 2013 2013 2013 <br><div dir="ltr" style="text-align: left;" trbidi="on"><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;"><br />var keyOne = "keyOne";<br />var keyTwo = "keyTwo";<br />var valueOne = "valueOne";<br />var valueTwo = "valueTwo";<br />// Map with two keys and two values<br />var map = {keyOne:valueOne,keyTwo:valueTwo}<br /><br />// Get function to get the value from key<br />function get(k){<br /> return map[k];<br />}<br /><br />// Create an array as shown below<br />var array = new Array(10);//It creates an empty array of length 10<br />array[0] = "Array Index 0";<br />array[1] = "Array Index 1";<br />array[2] = "Array Index 2";<br /><br />// Test Results<br />//alert("Value for key one in a map : "+get("keyOne"));<br />//alert("Value for key two in a map : "+get("keyTwo"));<br />//alert("Value from array : " + array[0]);<br /><br />var arrayWithElements = new Array(10,20,30); //It creates an with 3 elements<br />var arrayLiteral = [10,20,30]; //It creates an with 3 elements using the array literal<br /><br /></code></pre></br> In order to get the key and value from the map if you does not knows the key, then use following code. It may happen if there is a requirement to iterate through the map without having knowlege of keys that are stored in a map. In that case use the below code.</br><pre style="font-family:arial;font-size:12px;border:1px solid #CCCCCC;width:99%;height:auto;overflow:auto;background:#f0f0f0;;;padding:0px;color:#000000;text-align:left;line-height:20px;"><code style="color:#000000;word-wrap:normal;">for (var key in map) {<br /> //alert("Property \"" + key + "\" = " + map[key]);<br />}</code></pre> <!--Advertisment Starts--> <br /><table border="0" id="amazon_body_ads_com" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl-20&o=1&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B00DVFLJDS" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=B0075SUHWG&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=B006P88VSE&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?t=javacodeimpl-20&o=1&p=8&l=as1&asins=0316206849&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table> <table border="0" id="amazon_body_ads_in" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=8173666024&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=0006479901&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=1847370292&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://ws-in.amazon-adsystem.com/widgets/q?t=javacodeimpl-21&o=31&p=8&l=as1&asins=9350042096&ref=tf_til&fc1=000000&IS2=1&lt1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr&MarketPlace=IN&ServiceVersion=20070822&WS=1&ID=8042_ProductLink&Operation=GetProductLink&" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table> <table border="0" id="amazon_body_ads_uk" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B000C1ZDTU" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007VBGTX8" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B009SJCW3C" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-eu.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-21&o=2&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B009VK73FA" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td></tr></tbody></table> <table border="0" id="amazon_body_ads_ca" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width: 384px; display:none;"><tbody><tr height="20" style="height: 15.0pt;"> <td height="20" style="height: 15.0pt; width: 72pt;" width="96"><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007HCCOD0" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007FXPWCQ" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B007XZM6VG" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td><td>&nbsp;&nbsp;</td><td><iframe src="http://rcm-na.amazon-adsystem.com/e/cm?lt1=_blank&bc1=000000&IS2=1&bg1=FFFFFF&fc1=000000&lc1=0000FF&t=javacodeimpl00-20&o=15&p=8&l=as4&m=amazon&f=ifr&ref=ss_til&asins=B00834SJS0" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe></td> </tr></tbody></table></br> <script>//<![CDATA[ var tokens = document.domain.split('.'); var domain = tokens[tokens.length - 2]; if(tokens[tokens.length - 1]=="uk") { document.getElementById("amazon_body_ads_uk").style.display=""; } else if(tokens[tokens.length - 1]=="com") { document.getElementById("amazon_body_ads_com").style.display=""; } else if(tokens[tokens.length - 1]=="in") { document.getElementById("amazon_body_ads_in").style.display=""; } else if(tokens[tokens.length - 1]=="ca") { document.getElementById("amazon_body_ads_ca").style.display=""; } else{ document.getElementById("amazon_body_ads_com").style.display=""; } //]]></script></div><br>2013 2013 2013 <br> <a href="http://www.matrixar.com/" title="Matrix ">المصفوفة : أجمل الخلفيات والصور</a>

Admin
Admin

Posts : 64122
Join date : 2013-02-22

View user profile http://prowebsites1.forumaroc.net

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum