如何返回地图

时间:2019-03-05 10:58:33

标签: javascript async-await

我有一个采用以下方法的BooksDonation项目。

我遇到的问题是第三个方法(getAuthorsBookCount()),我不怎么写,我应该使用map,有人可以帮助我。 另外,您还可以检查我的代码吗?


getBook(bookName):返回书本对象(如果存在),否则返回null

getBooksWIthPageCountMoreThanX(pageCount): 返回所有pageCount大于给定pageCount的书籍。例如。如果用户使用pageCount = 1000调用该函数。它应该返回页数> 1000

的所有图书

getAuthorBooks(作者): 返回由该特定作者创作的所有书籍。 注意:有些书的作者不止一位。您也应该考虑这些因素,并将它们也退还。

getAuthorsBookCount(): 返回一个地图,其中包含作者姓名和其作者所著书的数量。


BooksRepo类

class BooksRepo {
    constructor() {
        this.fse = require('fs-extra');
        this.catalogFilePath = '../data/catalog.books.json';
    }

    async readFileAsync(filePath) {
        let data = await this.fse.readFile(filePath);
        let parsedData = await JSON.parse(data);
        return parsedData;
    }

    async getBook(bookName) {
        let books = await this.readFileAsync(this.catalogFilePath);
        let book = books.find(b => b.title == bookName);
        return book;
    }

    async getBooksWIthPageCountMoreThanX(pagecount){
        let books = await this.readFileAsync(this.catalogFilePath);
        let book = books.find(b => b.pageCount > pagecount);
        return book;
    }

    async getAuthorBooks(author){
        let books = await this.readFileAsync(this.catalogFilePath);
        let book = books.find(b => b.authors.contains(author));
        return book;
    }

    async getAuthorsBookCount(){
        let books = await this.readFileAsync(this.catalogFilePath);
        let author = books.find(b => b.authors );
        return book;
    }

    async getBooksbyCatagory(category){
        let books = await this.readFileAsync(this.catalogFilePath);
        let book = books.find(b => b.categories == category);
        return book;

    }
}

module.exports = BooksRepo ;

目录文件:结构示例

[
  {
    "_id": 1,
    "title": "Unlocking Android",
    "isbn": "1933988673",
    "pageCount": 416,
    "publishedDate": {
      "$date": "2009-04-01T00:00:00.000-0700"
    },
    "thumbnailUrl": "https://s3.amazonaws.com/AKIAJC5RLADLUMVRPFDQ.book-thumb-images/ableson.jpg",
    "shortDescription": "Unlocking Android: A Developer's Guide provides concise...",
    "longDescription": "Android is an open source mobile phone platform based on the Linux operating ...",
    "status": "PUBLISH",
    "authors": [
      "W. Frank Ableson",
      "Charlie Collins",
      "Robi Sen"
    ],
    "categories": [
      "Open Source",
      "Mobile"
    ]
  },

2 个答案:

答案 0 :(得分:3)

您可以像这样将标题作者转换为地图

const map = new Map();
  books.forEach((item)=>{
  if(item){
    const key = item.title;
    map.set(key,item.authors);
  }  });

答案 1 :(得分:2)

    var authArray = [];
    var authCountArray = [];

    for (let i = 0; i < inputJson.length; i++) {
        authArray = inputJson[i].authors;
        for (let j = 0; j < authArray.length; j++) {
            authCountArray[authArray[j]] = ((authCountArray[authArray[j]] === undefined) ? 0 : authCountArray[authArray[j]]) + 1;
        }
    }

   //we can use it like this
    getAuthorsBookCount(authName){
        return authCountArray[authName];
    }