forEach和each遍历数组的使用方式,一般用于遍历接收到的后台数据

// foreach遍历数组
  var arr = [{name:"limeng",email:"xfjylimeng"},{name:"hehe",email:"xfjylimeng"}];
   arr.forEach(myfun)
  function myfun(element,index,array){
   // element: 指向当前元素的值
// index: 指向当前索引
// array: 指向Array对象本身
     console.log(element.name);
  }

  // 遍历一个数组通常用each  一般从后台传入过来的数据基本是用each遍历
  $.each([{name:"limeng",email:"xfjylimeng"},{name:"hehe",email:"xfjylimeng"}],function(i,n)
  {
      // alert("索引:"+i+"对应值为:"+n.name);
      console.log(n.email);
  });

猜你喜欢

转载自my.oschina.net/u/3803573/blog/2873936
今日推荐