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输出