zoukankan      html  css  js  c++  java
  • 8-angualr.foreach

    为obj集合中的每一个项调用 iterator 函数,它可以是对象或数组。iterator 函数使用  iterator (value,key,obj)调用,其中value是对象属性对应的值或数组索引对应元素的值,key是对象属性或数组元素索引,而obj是obj本身。

      值得注意的是。forEach不会迭代继承的属性,因为它会使用hasOwnProperty方法进行筛选。

    遍历对象:

    var person = {name: 'misko', gender: 'male'};
    var log = [];
    angular.forEach(person, function(value, key) {
      this.push(key + ': ' + value);
    }, log);
    ---------------------------
    log=["name:misko","gender:male"]

    遍历数组:

    var objs =[{a:1},{a:2}];
    angular.forEach(objs, function(data,index,array){
      //data等价于array[index]
      console.log(data.a+'='+array[index].a);
    });

      参数如下:

        objs:需要遍历的集合

        data:遍历时当前的数据

        index:遍历时当前索引

        array:需要遍历的集合,每次遍历时都会把objs原样的传一次。

        也可以不用写后面两个参数:  

    var objs =[{a:1},{a:2}];
    angular.forEach(objs, function(data){
    console.log(data.a);
    });
  • 相关阅读:
    柔性数组成员 (flexible array member)-C99-ZZ
    如何阅读 Redis 源码?ZZ
    linux下网络编程学习——入门实例ZZ
    leetcode Ch2-Dynamic Programming [2014]
    leetcode Ch1-search 2014
    Skip List & Bloom Filter
    指针的引用-ZZ
    leetcode-sudoku solver
    rest framework之过滤组件
    rest framework之渲染器
  • 原文地址:https://www.cnblogs.com/ms-grf/p/6963499.html
Copyright © 2011-2022 走看看