Home > Web Front-end > JS Tutorial > Summary of methods for looping through Array and Map in JavaScript_Basic knowledge

Summary of methods for looping through Array and Map in JavaScript_Basic knowledge

WBOY
Release: 2016-05-16 15:10:59
Original
1297 people have browsed it

Various methods of looping arrays in js
eg1:

for (var i = 0; i < myStringArray.length; i++) { 
 alert(myStringArray[i]); 
 //Do something 
} 
Copy after login


eg2:

Array.prototype.foo = "foo!"; 
var array = ['a', 'b', 'c']; 
 
for (var i in array) { 
 alert(array[i]); 
} 
 
for(var i in this.$GLOBAL_DETAIL.album_photo_ids){if(this.$GLOBAL_DETAIL.album_photo_ids[i] == "3487675024077108") alert(this.$GLOBAL_DETAIL.album_photo_ids[i]);} 
Copy after login

eg3:

[1,2,3,4].map( function(item) { 
   alert(item); 
}) 
Copy after login


eg4:

var x = [1,2,3,4].map( function(item) { return item * 10; } ); 
 
// And now x is [10,20,30,40] 
Copy after login


eg5:

var myStringArray = [ "Hello", "World" ] 
var len = myStringArray.length 
for (var i=0; i<len; ++i) { 
 if (i in myStringArray) { 
 var s = myStringArray[i]; 
 ... do something with s ... 
 } 
} 
Copy after login


eg6:

var myStringArray = [ "Hello", "World" ] 
myStringArray.forEach( function(s) { 
  ... do something with s ... 
} ) 
Copy after login


eg7:

var i=0,item,items = ['one','two','three']; 
while(item=items[i++]){ 
 console.log(item); 
} 
 
// logs: 'one','two','three' 
 
and for the reverse order, an even more efficient loop 
var items = ['one','two','three'], i=items.length; 
while(i--){ 
 console.log(items[i]); 
} 
 
// logs: 'three','two','one' 
 
or the classical for loop 
var items = ['one','two','three'] 
for(var i=0,l=items.length; i < l ; i++){ 
 console.log(items[i]); 
} 
 
// logs: 'one','two','three' 
Copy after login


eg8:

var myStringArray = ['Hello', 'World']; // array uses [] not {} 
for (var i in myStringArray) { 
 console.log(i + ' -> ' + myStringArray[i]); // i is the index/key, not the item 
} 
Copy after login

js loop map to get all keys and values
eg1:

//Page地址 
 pageUrl : { 
  menu   : "loadPage.htm&#63;url=/collect/menu.page",   // 进入菜单页面 
  guangfaPage  : "loadPage.htm&#63;url=/collect/menu.page",   // 进入广发信息收集页面 
  pinganPage  : "loadPage.htm&#63;url=/collect/menu.page",   // 进入平安信息收集页面 
  nuonuoPage  : "loadPage.htm&#63;url=/collect/menu.page",   // 进入诺诺信息收集页面 
  youbangPage  : "loadPage.htm&#63;url=/collect/menu.page",   // 进入友邦信息收集页面 
  inputMobileNo : "loadPage.htm&#63;url=/collect/inputMobileNo.page", // 进入输入手机号页面 
  readIdCard  : "loadPage.htm&#63;url=/collect/readIdCard.page",  // 进入读取身份证页面 
  member   : "loadPage.htm&#63;url=/collect/member.page",   // 进入输入会员卡号页面 
  bankCard  : "loadPage.htm&#63;url=/collect/bankCard.page",   // 进入插入银行卡页面 
  url   : "loadPage.htm&#63;url=/collect/url.page"    // 进入跳转url页面 
 }, 
 
for(var key in this.pageUrl){ 
 alert(key+" : "+this.pageUrl[key]); 
} 
Copy after login


eg2:

var obj = { 
 "a": 1, 
 "b": 2, 
 "c": 3 
}; 
 
for (var prop in obj) { 
 if (obj.hasOwnProperty(prop)) { 
 // or if (Object.prototype.hasOwnProperty.call(obj,prop)) for safety... 
 alert("prop: " + prop + " value: " + obj[prop]) 
 } 
} 
Copy after login


Double Map Loop
eg:

var msg = ""; 
for(var key in Pin) { 
 for(var i in Pin[key]){ 
 msg+=i+": "+Pin[key][i]+"\n"; 
 } 
} 
alert(msg); 
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template