访问主阵列内的数组内的对象数据

时间:2016-03-19 08:53:22

标签: javascript jquery arrays json object

这是我的JSON数据 -

    allsurvey[
{eachquestion:
        [{question:sdvsd,
        answeroptions:[fsdf,sdf],
        questiontype:multiChoice},

        {question:sdf,
        answeroptions:sdf,
        questiontype:textbox}],
    noofques:2,
    surveytitle:aaaa
    }
]

我想访问数组中的值" eachquestion"即问题,答案选项

1 个答案:

答案 0 :(得分:1)

可能的数据结构可能就是这样。我添加了一个可以访问项目的示例:

var object = {
    allsurvey: [{
        eachquestion: [{
            question: 'sdvsd',
            answeroptions: ['fsdf', 'sdf'],
            questiontype: 'multiChoice'
        }, {
            question: 'sdf',
            answeroptions: 'sdf',
            questiontype: 'textbox'
        }],
        noofques: 2,
        surveytitle: 'aaaa'
    }]
};


object.allsurvey.forEach(function (a) {
    a.eachquestion.forEach(function (b) {
        document.write(Object.keys(b).map(function (k) {
            return k + ': ' + b[k];
        }).join('<br>'));
        document.write('<hr>');
    });
});