美文网首页
foreach循环json数组

foreach循环json数组

作者: Double杨_ | 来源:发表于2019-04-10 09:01 被阅读0次

定义一组数据

        var obj = [{
            name: 'a',
            age: 1
        }, {
            name: 'b',
            age: 2
        }, {
            name: 'c',
            age: 3
        }];

传入一个参数

        obj.forEach(function(element){
            console.log(element)
        })
image.png

传入一个参数时,foreach循环json数组中的对象

传入两个参数

        obj.forEach(function(element, index) {
            console.log(element)
            console.log(index)
        })
image.png
传入两个参数时,第一个参数是json数组中的对象,第二个参数是对应的index值

相关文章

网友评论

      本文标题:foreach循环json数组

      本文链接:https://www.haomeiwen.com/subject/asygiqtx.html