在Javascript对象中按ID检索数据

时间:2018-07-18 17:28:41

标签: javascript

假设我正在创建一个电子书阅读器。预加载到javascript对象中并在Angular 6中访问的数据结构是:

  Books Table:  
  id: 1, title: 'Cranes'  
  id: 2, title: 'Kittens'  
  id: 3, title: 'Hounds'  

  Chapters Table:  
  chapter-id: 1, book-id: 1, title: 'Tall mystics'  
  chapter-id: 2, book-id: 1, title: 'In flight'  
  chapter-id: 3, book-id: 1, title: 'Landing'  
  chapter-id: 4, book-id: 2, title: 'Meowers'  
  chapter-id: 5, book-id: 2, title: 'Purrers'  
  chapter-id: 6, book-id: 2, title: 'Poopers'  
  chapter-id: 7, book-id: 3, title: 'Floppy ears'  
  chapter-id: 8, book-id: 3, title: 'Howlers'  
  chapter-id: 9, book-id: 3, title: 'Attack!'  

  Pages Table:  
  id: 1, page-number: 1, book-id: 1, chapter-id: 1, page-content: 'They stand tall'  
  id: 2, page-number: 2, book-id: 1, chapter-id: 1, page-content: 'everywhere you go'  
  id: 3, page-number: 3, book-id: 1, chapter-id: 1, page-content: 'watch out!'  
  id: 4, page-number: 4, book-id: 1, chapter-id: 2, page-content: 'They fly high'  
  id: 5, page-number: 5, book-id: 1, chapter-id: 2, page-content: 'look up you all'  
  id: 6, page-number: 6, book-id: 1, chapter-id: 3, page-content: 'The crane has landed.  
  id: 7, page-number: 1, book-id: 2, chapter-id: 4, page-content: 'They meow all night.'  
  id: 8, page-number: 2, book-id: 2, chapter-id: 4, page-content: 'They meow all day.'  
  id: 9, page-number: 3, book-id: 2, chapter-id: 5, page-content: 'They purr all afternoon'  
  id: 10, page-number: 4, book-id: 2, chapter-id: 5, page-content: 'They purr all eve'  
  id: 11, page-number: 5, book-id: 2, chapter-id: 6, page-content: 'Handy sandbox to the rescue'  
  id: 12, page-number: 1, book-id: 3, chapter-id: 7, page-content: 'A floppy car ride'  
  id: 13, page-number: 2, book-id: 3, chapter-id: 8, page-content: 'Here comes a howl!'  
  id: 14, page-number: 3, book-id: 3, chapter-id: 9, page-content: 'Blood hound!'

当我的读者要求输入页面ID 3时,我该使用哪种方法来显示书名(Cranes)和章名(Tall mystics)?映射,过滤或查找是个好方法吗?

现在,我只是在跟踪用户选择的书,章和页面的ID-也许我不仅应该通过ID来跟踪状态,还应该通过章和书名来跟踪状态?在PHP世界中,我会使用关联数组,但是Java却无所适从。

0 个答案:

没有答案