在javascript中访问对象内的数组

时间:2014-08-18 12:19:58

标签: javascript arrays

我是angularjs的新手所以我不知道如何将任何值推送到特定位置的数组。例如:

$scope.atparticular.push = 
[

{questionText: 'queston1',questionId: '1',answerlst:[{answerText:'answer1',answerId: 'a1'},{answerText: 'answer2',answerId: 'a2'},{answerText: 'answer3',answerId: 'a3'}]},

{questionText: 'row2queston1',questionId: 'rq1',answerlst:[{answerText:'row2answer1',answerId: 'row2a1'},{answerText: 'row2answer2',answerId: 'row2a2'},{answerText: 'row2answer3',answerId: 'row2a3'}]}

];

如果这是我的包含列表的对象,并且我想追加另一个包含空值的位置的answerlst,例如:

$scope.atparticular.push = 
    [

    {questionText: 'queston1',questionId: '1',answerlst:[{answerText:'answer1',answerId: 'a1'},{answerText: 'answer2',answerId: 'a2'},{answerText: 'answer3',answerId: 'a3'},{answerText: '',answerId: ''}]},

    {questionText: 'row2queston1',questionId: 'rq1',answerlst:[{answerText:'row2answer1',answerId: 'row2a1'},{answerText: 'row2answer2',answerId: 'row2a2'},{answerText: 'row2answer3',answerId: 'row2a3'},{answerText: '',answerId: ''}]}

    ];

请提出一些建议。提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你想在数组answerlst的第一个元素的数组$scope.atparticular.push的末尾添加一个对象。

所以你想要:

$scope.atparticular.push[0].answerlst.push({answerText: '',answerId: ''});

请注意,这与angularJS本身无关。它只是简单的JavaScript。