我正在尝试在我的函数中实现一个helper函数,该函数试图对一系列书籍进行求解,并将其排序为前5名最受欢迎的书籍。出于某种原因,我不断收到一个语法错误,说明我在我的参数后缺少语法。我检查了我的代码,看起来一切正常,但也许我忽略了什么?任何帮助都将不胜感激。
这是给我的提示:
这个 getMostCommonGenres()
作用于 public/src/home.js
只有一个参数:
一系列的书。
它返回一个数组,该数组包含五个或更少的对象,这些对象表示最常见的类型,从最常见到最少排序。
返回数组中的每个对象都有两个键:
这个 name
表示流派名称的键。
这个 count
键,表示类型出现的次数。
如果存在五种以上的体裁,则只应返回前五种体裁。
以下是所用数据的一部分:
const books = [
{
id: "5f447132d487bd81da01e25e",
title: "sit eiusmod occaecat eu magna",
genre: "Science",
authorId: 8,
borrows: [
{
id: "5f446f2e2cfa3e1d234679b9",
returned: false,
},
{
id: "5f446f2ed3609b719568a415",
returned: true,
},
{
id: "5f446f2e1c71888e2233621e",
returned: true,
},
{
id: "5f446f2e6059326d9feb9a68",
returned: true,
},
{
id: "5f446f2ede05a0b1e3394d8b",
returned: true,
},
{
id: "5f446f2e4081699cdc6a2735",
returned: true,
},
{
id: "5f446f2e3900dfec59489477",
returned: true,
},
{
id: "5f446f2e6059326d9feb9a68",
returned: true,
},
{
id: "5f446f2e409f8883af2955dd",
returned: true,
},
{
id: "5f446f2e3900dfec59489477",
returned: true,
},
{
id: "5f446f2eae901a82e0259947",
returned: true,
},
{
id: "5f446f2ef2ab5f5a9f60c4f2",
returned: true,
},
{
id: "5f446f2ea6b68cf6f85f6e28",
returned: true,
},
{
id: "5f446f2eed18105706d6ca19",
returned: true,
},
{
id: "5f446f2eae901a82e0259947",
returned: true,
},
{
id: "5f446f2e91c2af00cb74e82b",
returned: true,
},
{
id: "5f446f2e5aa2bb5545a0f8a6",
returned: true,
},
{
id: "5f446f2ea508b6a99c3e42c6",
returned: true,
},
{
id: "5f446f2e50cc2da9cd80efdb",
returned: true,
},
{
id: "5f446f2e0b3e2ff72fc503e7",
returned: true,
},
{
id: "5f446f2e91c2af00cb74e82b",
returned: true,
},
{
id: "5f446f2ef795e593cd3cd19d",
returned: true,
},
{
id: "5f446f2e2f35653fa80bf490",
returned: true,
},
{
id: "5f446f2e7b9cd304fed3a8bc",
returned: true,
},
{
id: "5f446f2ed9aac23c0340aab2",
returned: true,
},
],
},
{
id: "5f4471329627160be1e8ce92",
title: "esse ea veniam non occaecat",
genre: "Classics",
authorId: 10,
borrows: [
{
id: "5f446f2ed3609b719568a415",
returned: false,
},
{
id: "5f446f2ec32d71dabec35b06",
returned: true,
},
{
id: "5f446f2ef2ab5f5a9f60c4f2",
returned: true,
},
{
id: "5f446f2e7a1be21e362b82f9",
returned: true,
},
{
id: "5f446f2e6059326d9feb9a68",
returned: true,
},
{
id: "5f446f2ec32d71dabec35b06",
returned: true,
},
{
id: "5f446f2e59f9380a1d03d766",
returned: true,
},
{
id: "5f446f2e141b97d842b680fd",
returned: true,
},
{
id: "5f446f2e409f8883af2955dd",
returned: true,
},
{
id: "5f446f2ee176f80b8d5d24da",
returned: true,
},
{
id: "5f446f2ef795e593cd3cd19d",
returned: true,
},
{
id: "5f446f2eef419207c5fa4ec9",
returned: true,
},
{
id: "5f446f2e50cc2da9cd80efdb",
returned: true,
},
{
id: "5f446f2e4081699cdc6a2735",
returned: true,
},
],
},
{
id: "5f44713265e5d8d17789beb0",
title: "tempor occaecat fugiat",
genre: "Travel",
authorId: 16,
borrows: [
{
id: "5f446f2e4eff1030e7316861",
returned: true,
},
{
id: "5f446f2ecc5c4787c403f844",
returned: true,
},
{
id: "5f446f2ee1661e64cde14e55",
returned: true,
},
{
id: "5f446f2e50cc2da9cd80efdb",
returned: true,
},
{
id: "5f446f2ef795e593cd3cd19d",
returned: true,
},
{
id: "5f446f2eae901a82e0259947",
returned: true,
},
{
id: "5f446f2e3e70bb4e1ab821c9",
returned: true,
},
{
id: "5f446f2ef2ab5f5a9f60c4f2",
returned: true,
},
{
id: "5f446f2e136866e4fe60c893",
returned: true,
},
],
},
{
id: "5f44713264bb872240dd62d0",
title: "proident cupidatat fugiat aliquip do",
genre: "Young Adult",
authorId: 20,
borrows: [
{
id: "5f446f2efa7fe184c4014dd2",
returned: false,
},
{
id: "5f446f2e59f9380a1d03d766",
returned: true,
},
{
id: "5f446f2ede05a0b1e3394d8b",
returned: true,
},
{
id: "5f446f2ed9aac23c0340aab2",
returned: true,
},
{
id: "5f446f2e136866e4fe60c893",
returned: true,
},
{
id: "5f446f2eef419207c5fa4ec9",
returned: true,
},
{
id: "5f446f2e409f8883af2955dd",
returned: true,
},
{
id: "5f446f2e59f9380a1d03d766",
returned: true,
},
{
id: "5f446f2e7d3e00a5efe35b6e",
returned: true,
},
{
id: "5f446f2ec32d71dabec35b06",
returned: true,
},
{
id: "5f446f2e3e70bb4e1ab821c9",
returned: true,
},
{
id: "5f446f2e5abedb2ba5eecd16",
returned: true,
},
{
id: "5f446f2e7eb4accbc92cb7a8",
returned: true,
},
{
id: "5f446f2ef7fc711c78b57e51",
returned: true,
},
{
id: "5f446f2e5a23764338868b10",
returned: true,
},
{
id: "5f446f2ea0502bf8cbc7676a",
returned: true,
},
{
id: "5f446f2eaf32d8ddf09e8f83",
returned: true,
},
{
id: "5f446f2eef419207c5fa4ec9",
returned: true,
},
{
id: "5f446f2e91a138c3a8ac6980",
returned: true,
},
{
id: "5f446f2e5e2952040e9f9b88",
returned: true,
},
{
id: "5f446f2e141b97d842b680fd",
returned: true,
},
{
id: "5f446f2ee734f727dfceefbd",
returned: true,
},
{
id: "5f446f2efa2e5110917e5929",
returned: true,
},
{
id: "5f446f2e5e2952040e9f9b88",
returned: true,
},
{
id: "5f446f2e4081699cdc6a2735",
returned: true,
},
{
id: "5f446f2e7b9cd304fed3a8bc",
returned: true,
},
],
},
{
id: "5f447132a476ece080afa067",
title: "ullamco est minim",
genre: "Nonfiction",
authorId: 25,
borrows: [
{
id: "5f446f2e189628dfd4e6225e",
returned: false,
},
{
id: "5f446f2ec56b2fa77d5545ef",
returned: true,
},
{
id: "5f446f2e2f35653fa80bf490",
returned: true,
},
{
id: "5f446f2ee1661e64cde14e55",
returned: true,
},
{
id: "5f446f2ee5be00208a4481e0",
returned: true,
},
{
id: "5f446f2ecc5c4787c403f844",
returned: true,
},
{
id: "5f446f2e91c2af00cb74e82b",
returned: true,
},
{
id: "5f446f2e2f35653fa80bf490",
returned: true,
},
{
id: "5f446f2ed92454081fb1999b",
returned: true,
},
{
id: "5f446f2e136866e4fe60c893",
returned: true,
},
{
id: "5f446f2e7d3e00a5efe35b6e",
returned: true,
},
{
id: "5f446f2e93963fcd4e5adf00",
returned: true,
},
{
id: "5f446f2e4081699cdc6a2735",
returned: true,
},
{
id: "5f446f2ecc5c4787c403f844",
returned: true,
},
{
id: "5f446f2e136866e4fe60c893",
returned: true,
},
{
id: "5f446f2ef055e8f3064b763e",
returned: true,
},
{
id: "5f446f2e7d3e00a5efe35b6e",
returned: true,
},
{
id: "5f446f2e5e2952040e9f9b88",
returned: true,
},
{
id: "5f446f2ed9aac23c0340aab2",
returned: true,
},
{
id: "5f446f2efa2e5110917e5929",
returned: true,
},
{
id: "5f446f2e3e70bb4e1ab821c9",
returned: true,
},
{
id: "5f446f2e6dd33178245e5771",
returned: true,
},
{
id: "5f446f2ec56b2fa77d5545ef",
returned: true,
},
{
id: "5f446f2eef419207c5fa4ec9",
returned: true,
},
{
id: "5f446f2ef055e8f3064b763e",
returned: true,
},
{
id: "5f446f2e1c71888e2233621e",
returned: true,
},
{
id: "5f446f2eeb9463f976413b65",
returned: true,
},
{
id: "5f446f2e138a3841f7aeccf2",
returned: true,
},
{
id: "5f446f2e4081699cdc6a2735",
returned: true,
},
],
}
以下是我尝试过的:
function helper(books) {
let countObj = {};
books.forEach(aBook => {
if (countObj[aBook.genre] != null) {
countObj[aBook.genre]++;
} else {
countObj[aBook.genre] = 1;
}
} //This is the line where the error is occurring
return countObj;
}
}
function getMostCommonGenres(books) {
let countObj = helper(books);
let countArray = [];
for (const [key, value] of Object.entries(countObj)) {
countArray.push({
'name': key,
'count': value
});
}
countArray.sort((a, b) => b.count - a.count);
return countArray.slice(0, 5);
}
我正在尝试用一个helper函数来解决这个提示,我知道它不需要helper函数,但这就是我要添加的,再次感谢任何帮助。谢谢
1条答案
按热度按时间sr4lhrrt1#
这只是你方括号的一个小问题: