简体   繁体   English

如何将空数组添加到对象数组?

[英]How do I add an empty array to an object array?

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 = []

gets items added via a for loop like this: 获取通过for循环添加的项目,如下所示:

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

that works great and I can get what I need from myArray.libraries .name . 这很好用,我可以从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: 我只需要添加books这样我就可以遍历该库中的每本书:

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

I tried using myArray.libraries.books = [] but that didn't add the collect to each library. 我尝试使用myArray.libraries.books = []但没有将collect添加到每个库。 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. 我也尝试了myArray.libraries.push({ books: { } })但是这没有任何影响并且给了我一个错误,我无法推送到myArray.libraries.books因为书籍不存在。

EDIT 编辑

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

EDIT 编辑

I ran some tests and it turns out when I do my second database call it doesn't know the original $scope exists. 我运行了一些测试,结果当我进行第二次数据库调用时,它不知道原始的$scope存在。

Try as follows: 尝试如下:

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

That should do the trick. 这应该够了吧。

Basically the problem is that you are trying to push array to an array. 基本上问题是你正在尝试将数组推送到数组。 You can push another array to your libraries , but to access books you have to use index to access specific item like myArray.libraries[0].books[0] 您可以将另一个数组推送到您的libraries ,但要访问书籍,您必须使用索引来访问特定项目,如myArray.libraries[0].books[0]

So I assume you have to change myArray.libraries.push({ books: { } }) to myArray.libraries.push({ books: [] }) 所以我假设您必须将myArray.libraries.push({ books: { } })更改为myArray.libraries.push({ books: [] })

I have created a plunker for you. 我为你创造了一个plunker。 Please refer to 请参阅

" http://plnkr.co/edit/0FKg2fbe4CY11Oz6aSBF?p=preview " http://plnkr.co/edit/0FKg2fbe4CY11Oz6aSBF?p=preview

$scope.libraries = {
    books : [
  {"name" : "book1"},
  {"name" : "book2"},
  {"name" : "book3"}
      ]


  };

This will help you. 这会对你有所帮助。

DB calls are async, so the 2nd db call shouldn't be in loop. 数据库调用是异步的,因此第二个数据库调用不应该在循环中。 Please check if this works 请检查一下是否有效

$scope.libraries = [];  

  init();

  function init() {
    // do the first db transaction
  }

  // transaction1 callback
  function trans1Callback(result) {    
    addBooks(result);
  }

  var i = 0;

  function addBooks(result) {
      // to stop iteration at the end of records
      if (i >= result.rows.length) {
        return;
      }
      var item = result.rows.item(i);
      var data = {name: item.name, state: item.state, books: []};
      // do the second db transaction

      // transaction2 callback
      function trans2Callback(result2) {

          for (var j =0; j < result2.rows.length; j++) {
              var book = result.rows.item(j);
              data.books.push({ name: book.prodName });
          }
          // push to scope libraries
          $scope.libraries.push(data);
          // do increment
          i++;
          // Call addBooks again to process next record
          addBooks(result);
      }

  }

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

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