繁体   English   中英

for循环中的javaScript数组对象

[英]javaScript array object in for loop

我有一个关于对象的 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'

}

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

您应该首先定义一个数组,如$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