简体   繁体   English

另一个数组内的Javascript数组

[英]Javascript array inside another array

I am starting with the ionic framework and I am stuck in creating and calling an array inside another array. 我从离子框架开始,我被困在另一个数组中创建和调用一个数组。

I have the following code in my books.js : 我的books.js有以下代码:

.factory('Books', function() {

  // books data
  var books = [{
    id: 0,
    title: 'Sample Title',
    author: 'Sample Author',
    category: 'Horor, Fiction',
    cover: '/cover.jpeg',
    details: 'some details about the book',
    // my first attempt to create an array to store chapters info
    chapters: [
      {
        name: 'Chapter 1',
        filename: 'chapter1.html',
      },
      {
        name: 'Chapter 2',
        filename: 'Chapter2.html',
      }
    ]
  }, {
    id: 1,
    title: 'Sample Title Two',
    author: 'Sample Author two',
    category: 'Horor, Fiction',
    cover: '/cover.jpeg',
    details: 'some details about the book',
    // my first attempt to create an array to store chapters info
    chapters: [
      {

        name: 'Chapter 1',
        filename: 'chapter1.html',
      },
      {
        name: 'Chapter 2',
        filename: 'Chapter2.html',
      }
    ]
  },

when accessing data, I use {{book.title}} and it will output the title of the book. 访问数据时,我使用{{book.title}} ,它将输出书名。 But I am not sure how to return the chapters values since each book has different numbers of chapters. 但是我不确定如何返回章节值,因为每本书都有不同数量的章节。

Perhaps assign a chapter ID for each chapter and call that ID ? 也许为每个章节分配一个章节ID并调用该ID?

Try this to show it on Angular 尝试在Angular上显示它

<div ng-repeat="book in books">
    {{book.title}}
    <div ng-repeat="chapter in book.chapters">
        {{chapter.name}}
    </div>
</div>

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

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