forEach

来源:2-6 首页-类目(2)

soso_crazy

2019-06-04 19:18:46

function initContentList(list) {
   list.forEach(function (item,index) {
       var str=itemTmpl
           .replace('$pic_url',item.pic_url)
           .replace('$name',item.name)
           .replace('$distance',item.distance)
           .replace('$min_price_tip',item.min_price_tip)
           .replace('$mt_delivery_time',item.mt_delivery_time)
           .replace('$brand',getBrand(item))
           .replace('$monthNum',getMonthNum(item))
           .replace('$others',getOthers(item));

       $('.list-wrap').append($(str));
   });
}


forEach 遍历和for(var i0; i<lenght)的那种遍历有什么区别?

写回答

2回答

好帮手慕星星

2019-06-05

你好,list是一个数组哦,调用的时候传入,可以输出看一下:

http://img.mukewang.com/climg/5cf734e60001b38408390413.jpg

http://img.mukewang.com/climg/5cf735110001641b07030037.jpg

list是一个数组,只不过数组中每一项是对象而已。

自己可以测试下.

0

好帮手慕星星

2019-06-05

你好,可以参考下面的解释:

(1)forEach只能用于循环数组:

http://img.mukewang.com/climg/5cf71fce0001b02b04920478.jpg

例如:

http://img.mukewang.com/climg/5cf72054000143d304820155.jpg

(2)for循环是js中常用的,用于数组或者元素集合。例如:

http://img.mukewang.com/climg/5cf721100001d9e806510308.jpg

自己测试下,祝学习愉快!

0
hoso_crazy
h list.forEach(function (item,index) { var str=itemTmpl .replace('$pic_url',item.pic_url) .replace('$name',item.name) .replace('$distance',item.distance) .replace('$min_price_tip',item.min_price_tip) .replace('$mt_delivery_time',item.mt_delivery_time) .replace('$brand',getBrand(item)) .replace('$monthNum',getMonthNum(item)) .replace('$others',getOthers(item)); 例子中的forEach遍历的不是数组,是对象。不是说forEach只能遍历数组的吗?
h019-06-05
共1条回复

0 学习 · 6815 问题

查看课程