JavaScript中循环遍历Array与Map的方法小结

前端之家收集整理的这篇文章主要介绍了JavaScript中循环遍历Array与Map的方法小结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

js循环数组各种方法

eg1:

eg2:

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]);}

eg3:

eg4:

// And now x is [10,20,30,40]

eg5:

eg6:

eg7:

// logs: 'one','three'

and for the reverse order,an even more efficient loop
var items = ['one','three'],i=items.length;
while(i--){
console.log(items[i]);
}

// logs: 'three','one'

or the classical for loop
var items = ['one','three']
for(var i=0,l=items.length; i < l ; i++){
console.log(items[i]);
}

// logs: 'one','three'

eg8:

' + myStringArray[i]); // i is the index/key,not the item }

js循环map,获取所有的key和value

eg1:

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

eg2:

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])
}
}

双重Map循环

eg:

原文链接:https://www.f2er.com/js/49706.html

猜你在找的JavaScript相关文章