for循环

时间:2015-08-10 15:22:16

标签: javascript arrays angularjs listview material

我有一个关于对象的javaScript数组的问题:

如下所示:

$scope.todos = [
    {
        face : imagePath,
        what: 'Das',
        who: 'Sophia',
        when: '3:08PM',
        notes: " Description 1",
        linkForward: "#/tab/listView1"
    },
    {
        face : imagePath,
        what: 'Dis',
        who: 'Emma',
        when: '3:08PM',
        notes: " Description 1",
        linkForward: "#/tab/listView2"
    },
    {
        face : imagePath,
        what: 'Dos',
        who: 'Olivia',
        when: '3:08PM',
        notes: " Description 1",
        linkForward: "#/tab/listView3"
    }
];

我想在for循环中推送所有这些项目:

应该看起来像:

for(var i = 0; i < 3; i++){
    $scope.todos[i].face = 'image Path'
    $scope.todos[i].what= 'image Path'
    $scope.todos[i].who= 'image Path'
    $scope.todos[i].when= 'image Path'
    $scope.todos[i].linkForward= 'image Path'

}

但它不起作用,我想动态创建这个数组。

1 个答案:

答案 0 :(得分:3)

您应首先定义一个数组,如$scope.todos = []&amp;更好的方法就像设置如下所示的数组。

$scope.todos = []
for(var i = 0; i < 3; i++){
    $scope.todos.push({
       face: 'image Path', 
       what : 'image Path', 
       who: 'image Path', 
       when: 'image Path', 
       linkForward: 'image Path'
    });
};