当前位置: 代码迷 >> 综合 >> js遍历集合
  详细解决方案

js遍历集合

热度:107   发布时间:2023-10-24 05:49:28.0

js遍历集合

方式1

普通的for循环

假设后台传过来的是一个对象集合data
 

for(var i=0;i<data.length;i++){data[i].name}

方式2

利用for in 循环遍历集合

for(item in data){data[item].name
}

易错点:此item是集合的下标并不是集合的元素

方式3

利用  $.each(data,function(index,item){})

$.each(data,function(index,item){item.name
})

注意data是被遍历的集合

item是集合中的元素

index是遍历集合的下标

 

  相关解决方案