简体   繁体   English

for循环中的javaScript数组对象

[英]javaScript array object in for loop

I have a question about javaScript array of object:我有一个关于对象的 javaScript 数组的问题:

It looks like below:它看起来像下面:

$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"
    }
];

I want to push all this items in a for loop:我想在 for 循环中推送所有这些项目:

It should look like :它应该看起来像:

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'

}

But it doesn't work, I want to create this array dynamically.但它不起作用,我想动态创建这个数组。

You should define an array first like $scope.todos = [] & better way would be like setting array like below.您应该首先定义一个数组,如$scope.todos = [] & 更好的方法是像下面这样设置数组。

$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'
    });
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM