[轉] Javascript中數組與字典(即map)的使用

來源:互聯網
上載者:User

標籤:

簡述:

簡單記錄一下資料結構Map和數組,

其實在Javascript這種弱類型的指令碼語言中,數組同時也就是字典,下面主要就是字典數組的簡易使用


代碼:

1. 數組中添加map

 

[html] view plaincopy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5. <title>Test</title>  
  6. <script type="text/javascript">  
  7.   
  8. var arr = [];  
  9. var key = ‘Jeremy‘;  
  10. var value = ‘!!!!‘  
  11. arr.push({  
  12.     ‘key‘: key,  
  13.     ‘value‘: value,  
  14. });  
  15.   
  16. document.write("key: " + arr[0][‘key‘] +  
  17.         "<br/>value: " + arr[0][‘value‘]);  
  18.   
  19. </script>  
  20. </head>  
  21. <body>  
  22.   
  23. </body>  
  24. </html>  

輸出0:


2. 數組遍曆輸出 [html] view plaincopy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5. <title>Test</title>  
  6. </head>  
  7. <body>  
  8. <script type="text/javascript">  
  9. var arr = [];  
  10. arr.push("Jeremy");  
  11. arr.push("Jimmy");  
  12. for(var i in arr)  
  13.     document.write(i + ": " + arr[i] + "</br>");  
  14. </script>  
  15. </body>  
  16. </html>  

輸出1:


3. 類似字典(map)遍曆 [html] view plaincopy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5. <title>Test</title>  
  6. </head>  
  7. <body>  
  8. <script type="text/javascript">  
  9. var dict = []; //or dict = new Array()  
  10. dict["Jeremy"] = 20;  
  11. dict["Jimmy"] = 30;  
  12. for(var key in dict)  
  13.     document.write(key + ": " + dict[key] + "</br>");  
  14. </script>  
  15. </body>  
  16. </html>  

輸出2:



4. 字典聲明時賦值
[java] view plaincopy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5. <title>Test</title>  
  6. </head>  
  7. <body>  
  8. <script type="text/javascript">  
  9. var dict = {  
  10.     "Jeremy" : 20,  
  11.     "Jimmy" : 30  
  12. };  
  13. for(var key in dict)  
  14.     document.write(key + ": " + dict[key] + "</br>");  
  15. </script>  
  16. </body>  
  17. </html>  

輸出3:


5.字典中嵌套數組 [html] view plaincopy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5. <title>Test</title>  
  6. </head>  
  7. <body>  
  8. <script type="text/javascript">  
  9. var dict = {  
  10.     "Jeremy" : ["Chinese", "Math"] ,  
  11.     "Jimmy" : ["Art", "English"]  
  12. };  
  13. var name = "Jeremy";  
  14. for(var courseIndex in dict[name])  
  15.     document.write(dict[name][courseIndex] + "</br>");  
  16. </script>  
  17. </body>  
  18. </html>  

輸:4:



6. 字典裡value為數組, 數組內為字典,

下面的邏輯就是學生 :  課程列表 : 某門的課程資訊

[html] view plaincopy
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  5. <title>Test</title>  
  6. </head>  
  7. <body>  
  8. <script type="text/javascript">  
  9. var dict = [];  
  10. var courseListOfJeremy = [  
  11.     {"Chinese" : 3},   
  12.     {"Math": 5}  
  13. ];  
  14. dict[‘Jeremy‘] = courseListOfJeremy;  
  15. var courseListOfJimmy =  [  
  16.     {"Art": 3},   
  17.     {"English": 5}  
  18. ];  
  19. dict[‘Jimmy‘] = courseListOfJimmy;  
  20.   
  21. document.write("Jimmy‘s Course Number Of Chinese: " + dict[‘Jeremy‘][0][‘Chinese‘]);  
  22. </script>  
  23. </body>  
  24. </html>  

輸出5:


[轉] Javascript中數組與字典(即map)的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.