JavaScript中循环遍历Array与Map的方法小结
程序员文章站
2023-11-21 13:14:34
js循环数组各种方法
eg1:
for (var i = 0; i < mystringarray.length; i++) {
alert(m...
js循环数组各种方法
eg1:
for (var i = 0; i < mystringarray.length; i++) { alert(mystringarray[i]); //do something }
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]);}
eg3:
[1,2,3,4].map( function(item) { alert(item); })
eg4:
var x = [1,2,3,4].map( function(item) { return item * 10; } ); // and now x is [10,20,30,40]
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 ... } }
eg6:
var mystringarray = [ "hello", "world" ] mystringarray.foreach( function(s) { ... do something with s ... } )
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'
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 }
js循环map,获取所有的key和value
eg1:
//page地址 pageurl : { menu : "loadpage.htm?url=/collect/menu.page", // 进入菜单页面 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:
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]) } }
双重map循环
eg:
var msg = ""; for(var key in pin) { for(var i in pin[key]){ msg+=i+": "+pin[key][i]+"\n"; } } alert(msg);
上一篇: js的预解析和作用域
下一篇: 不同浏览器的内核
推荐阅读
-
JavaScript中循环遍历Array与Map的方法小结
-
JavaScript遍历数组的三种方法map、forEach与filter实例详解
-
JavaScript中循环遍历Array与Map的方法小结
-
JavaScript 中的12种循环遍历方法【总结】
-
JavaScript中关于indexOf的使用方法与问题小结_基础知识
-
JS中数组与对象的遍历方法实例小结
-
JavaScript遍历数组的三种方法map、forEach与filter实例详解
-
js中的数组Array定义与sort方法使用示例_javascript技巧
-
详解Java中list,set,map的遍历与增强for循环代码实例
-
js中的数组Array定义与sort方法使用示例_javascript技巧