JS中对List、Map的各种遍历方式
2018-02-13 10:57
609 查看
var list1 = ["number","name"];
var list2 = ["36","Crown","15","Faker","Swift","68","Dandy"];
var map_demo = { name: "John", lang: "JS" };
1.最常用的for循环
for(var i=0;i<list2.length;i++){
console.info(i +":"+ list2 [i]);
}
改进:这里可以将list2.length提出来,不用每次计算长度,效率更高一些,such as:
var len=list2.length;
for(var i=0;i<len;i++){
console.info(i +":"+ list2 [i]);
}
小结:很常见也很常用,效率也不差,但不能遍历map。
2.for...in...遍历List/map
//遍历map
for(var key in map_demo){
console.info(key+":"+map_demo[key]);
}
//遍历List
for(var index in list2){
console.info(index+":"+list2[index]);
}
小结:对于List来说,能不用for...in就不要用,效率低下。
3.forEach遍历List
list2.forEach(function (element, index, array) {
console.info(element); //当前元素的值
console.info(index); //当前下标
console.info(array); //数组本身
});
小结:和for循环效率差不多。
4.$.each()遍历List/map
//遍历List
$.each(list2,function(index,items){
console.info(index+":"+items);
});
//遍历map
$.each(map_demo,function(key,value){
console.info("key: " + key + ", Value: " + value );
})
5.$.map()遍历List/map
//遍历List
var new_list = $.map(list2,function(items,index){
return items+"!";
})
console.info(new_list);
//遍历map
$.map(map_demo,function(key,value){
console.log(key+":"+value);
});
小结:$.map()写法和$.each()类似,但对list的遍历时,参数顺序和$.each()是相反的,并且可以带返回值。对map的遍历和$.each()一样
var list2 = ["36","Crown","15","Faker","Swift","68","Dandy"];
var map_demo = { name: "John", lang: "JS" };
1.最常用的for循环
for(var i=0;i<list2.length;i++){
console.info(i +":"+ list2 [i]);
}
改进:这里可以将list2.length提出来,不用每次计算长度,效率更高一些,such as:
var len=list2.length;
for(var i=0;i<len;i++){
console.info(i +":"+ list2 [i]);
}
小结:很常见也很常用,效率也不差,但不能遍历map。
2.for...in...遍历List/map
//遍历map
for(var key in map_demo){
console.info(key+":"+map_demo[key]);
}
//遍历List
for(var index in list2){
console.info(index+":"+list2[index]);
}
小结:对于List来说,能不用for...in就不要用,效率低下。
3.forEach遍历List
list2.forEach(function (element, index, array) {
console.info(element); //当前元素的值
console.info(index); //当前下标
console.info(array); //数组本身
});
小结:和for循环效率差不多。
4.$.each()遍历List/map
//遍历List
$.each(list2,function(index,items){
console.info(index+":"+items);
});
//遍历map
$.each(map_demo,function(key,value){
console.info("key: " + key + ", Value: " + value );
})
5.$.map()遍历List/map
//遍历List
var new_list = $.map(list2,function(items,index){
return items+"!";
})
console.info(new_list);
//遍历map
$.map(map_demo,function(key,value){
console.log(key+":"+value);
});
小结:$.map()写法和$.each()类似,但对list的遍历时,参数顺序和$.each()是相反的,并且可以带返回值。对map的遍历和$.each()一样
相关文章推荐
- js Map List 遍历
- js Map List 遍历使用示例
- java中list、set、map的各种遍历方法
- Java中list.map.set基本概念,方法比较和遍历方式
- java中List、Set、Map集合遍历的几种方式小结和比较
- List及Map遍历的几种方式
- 遍历List、Set、Map集合的几种方式
- 各种不同方式遍历map
- Java之List和Map的几种遍历方式
- Set、List、Map的遍历的几种方式
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- 分析轮子(八)- List.java 各种遍历方式及遍历时移除元素的方法
- java中list,set,Map集合的遍历方式
- 遍历list、set和map集合的方式
- List 、Set 、Map 的遍历方式
- struts标签遍历各种list Map
- 集合List、Set、Map的遍历方式
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- js Map List 遍历使用示例
- List,Set,Map遍历方式 (收集的资源,值得看一下)