如何添加一个空数组对象数组? [英] How do I add an empty array to an object array?

查看:178
本文介绍了如何添加一个空数组对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有我拉从数据的数组。我想一个新的阵列添加到它,所以我可以拉更多的数据。

basically I have an array that I pull data from. I would like to add a new array to it so I can pull additional data.

var myArray.libraries = []

获得通过添加for循环这样的项目:

gets items added via a for loop like this:

for(var x=0; x < items.length; x++){
    myArray.libraries ({ name: items(x).name, state: items(x).state});
}

这伟大工程,我能得到什么,我从 myArray.libraries。名称需要。我需要添加,如书这样的东西每个条目可以有它的书单。可能有1个或更多的书。

that works great and I can get what I need from myArray.libraries .name. I need to add something like "books" so each entry can have it's list of books. There could be 1 or more books.

要清楚,我采用了棱角分明让我用这样的输出为每个库:

to be clear I'm using angular so I use something like this to output for each library:

<div ng-repeat="library in libraries">
    <p>{{library.name}}</p>
</div>

我只需要添加图书所以我可以通过每本书在图书馆循环:

I just need to add books so I can loop through each book in that library:

<div ng-repeat="book in libraries.books">
    <p>{{book.name}}</p>
</div>

我试着用 myArray.libraries.books = [] ,但没有添加收集到的每个库。我也试过 myArray.libraries.push({书:{}})但没有任何影响,而且给了我,我不能推到一个错误 myArray.libraries.books ,因为书籍是不存在的。

I tried using myArray.libraries.books = [] but that didn't add the collect to each library. I also tried myArray.libraries.push({ books: { } }) but that didn't have any affect and gave me an error that I couldn't push to myArray.libraries.books since books didn't exist.

修改

下面是一些示例code。我使用的角度,但原则应该是相同的:

Here's some sample code. I'm using angular but the principle should be the same:

$scope.libraries= [];

//start loop to get the installations
for (var x = 0; x < res.rows.length; x++) {
    $scope.libraries.push({ ... , books: []});

    //run a new query to get the additional libraries info
    var booksQuery = query to get books;
    //run query to get results and loop through them
        for (var i = 0;i < res2.rows.length; i++) {
            $scope.libraries[x].books.push({ name: res2.rows.item(i).prodName });
        }
    });
}

修改

我跑了一些测试,事实证明,当我做我的第二个数据库调用,它不知道原来的 $范围存在。

I ran some tests and it turns out when I do my second database call it doesn't know the original $scope exists.

推荐答案

尝试如下:

for(var x=0; x < items.length; x++){
    myArray.libraries ({ name: items(x).name, state: items(x).state, books: []});
}

这是应该做的伎俩。

这篇关于如何添加一个空数组对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆