JS中的forEach使用

forEach方法中的function回调支持3个参数:
value:遍历的数组内容;index:对应的数组索引,array:数组本身。

[].forEach(function(value, index, array) {
  // ...
});

例:

data数据

{"posts":[{"userID":1,"userName":"linger","password":"linger","gender":0,"identity":1},{"userID":2,"userName":"andre","password":"andre","gender":0,"identity":2},{"userID":3,"userName":"admin","password":"admin","gender":0,"identity":3},{"userID":4,"userName":"shumei","password":"shumei","gender":1,"identity":2}]}

js语句

data.posts.forEach(function (value, , array) {
                console.log(value);
                console.log(index);
                console.log(array);
            })

console输出


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 工厂模式类似于现实生活中的工厂可以产生大量相似的商品,去做同样的事情,实现同样的效果;这时候需要使用工厂模式。简单...
    舟渔行舟阅读 7,842评论 2 17
  • JS使用技巧专题 1开发技巧 1.1函数使用 1.1.1函数声明方式 JS函数的写法总结 http://blog....
    Kevin_Junbaozi阅读 1,110评论 0 11
  • 单例模式 适用场景:可能会在场景中使用到对象,但只有一个实例,加载时并不主动创建,需要时才创建 最常见的单例模式,...
    Obeing阅读 2,104评论 1 10
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,962评论 19 139
  • =========================================================...
    lavor阅读 3,508评论 0 5