gpt4 book ai didi

javascript - 如何按流行度从数组中获取前五个对象

转载 作者:行者123 更新时间:2023-12-04 14:54:04 25 4
gpt4 key购买 nike

描述:

getMostPopularAuthors 返回一个包含五个或更少对象的数组,这些对象代表最受欢迎的作者,他们的书被 checkout 次数最多。受欢迎程度的表示方法是找到作者所写的所有书籍,然后将这些书籍被借阅的次数相加。

函数有两个参数,顺序如下:

  • 一系列书籍。
  • 一组作者。

返回数组中的每个对象都有两个键:

  • 代表作者名字和姓氏的 name 键。
  • count键,代表作者的书被借阅的次数。

如果出现的作者超过五位,则只返回前五名。

注意:

我可以解决答案的第二部分,但我在解决第一部分问题。我知道它想访问 author.js 文件,但它也想访问 books.js 文件,我不知道如何实现和访问 author.js 数据.

这是 author.js 文件数据的一部分:

const authors = [
{
id: 0,
name: {
first: "Lucia",
last: "Moreno",
},
},
{
id: 1,
name: {
first: "Trisha",
last: "Mathis",
},
},
{
id: 2,
name: {
first: "Arnold",
last: "Marks",
},
},
{
id: 3,
name: {
first: "Faye",
last: "Arnold",
},
},
{
id: 4,
name: {
first: "Tami",
last: "Hurst",
},
},
{
id: 5,
name: {
first: "Farmer",
last: "Stevenson",
},
},
{
id: 6,
name: {
first: "Hancock",
last: "Fuller",
},
},
{
id: 7,
name: {
first: "Ila",
last: "Reid",
},
},
{
id: 8,
name: {
first: "Susanne",
last: "Lawson",
},
}
]

这是 book.js 文件数据的一部分:

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,
},
],
},
]

这是我尝试过的:

function getMostPopularAuthors(books, authors) {
const authorPopularity = books.map((book) => book.authorId);
const temp = [];
authorPopularity.map((authorId) => {
const borrowLocation = temp.findIndex((element) => element.name === authorId);
if (borrowLocation >= 0) {
temp[borrowLocation].count = temp[borrowLocation].count + 1;
} else {
temp.push({ name: authorId, count: 1 });
}
});
temp.sort((alpha, delta) => delta.count - alpha.count);
if (temp.length > 5) {
return temp.slice(0, 5);
}
return temp;
}

此外,如果有人想要引用测试代码,请在此处:

describe("getMostPopularAuthors()", () => {
it("should return an ordered list of most popular authors", () => {
const actual = getMostPopularAuthors(books, authors);
const [first, second] = [
{ name: "Susanne Lawson", count: 11 },
{ name: "Matthews Sanders", count: 5 },
];
expect(actual[0]).to.eql(first);
expect(actual[1]).to.eql(second);
});

it("should limit the list to the top five", () => {
const actual = getMostPopularAuthors(books, authors);
expect(actual.length).to.equal(5);
});});});

最佳答案

您可以 map() 作者数组并使用 filter() 找到该作者的所有书籍。对于计数,您可以使用 reduce 将每个作者借用数组的长度加在一起。

function getPopularAuthors(authorArray, booksArray, total) {
return authorArray.map(a => {
// loop through the author array
a.count = booksArray.filter(b => b.authorId === a.id).reduce((b, a) => b + (a.borrows && a.borrows.length || 0), 0);
// to get the count, filter the book array and then reduce it to a sum of all matching books borrows array length
a.name = `${a.name.first} ${a.name.last}`;
delete a.id
// remove the id since that isn't part of the desired result
return a
}).sort((a, b) => b.count - a.count).slice(0, total)
// sort the end result to be biggest counts first, then slice the array down to the desired length
}

const authors = [{
id: 0,
name: {
first: "Lucia",
last: "Moreno",
},
},
{
id: 1,
name: {
first: "Trisha",
last: "Mathis",
},
},
{
id: 2,
name: {
first: "Arnold",
last: "Marks",
},
},
{
id: 3,
name: {
first: "Faye",
last: "Arnold",
},
},
{
id: 4,
name: {
first: "Tami",
last: "Hurst",
},
},
{
id: 5,
name: {
first: "Farmer",
last: "Stevenson",
},
},
{
id: 16,
name: {
first: "Hancock",
last: "Fuller",
},
},
{
id: 7,
name: {
first: "Ila",
last: "Reid",
},
},
{
id: 8,
name: {
first: "Susanne",
last: "Lawson",
},
}
]

const books = [{
id: "test",
title: "Test with no borrows",
genre: "Science",
authorId: 8},
{
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,
},
],
}
]

function getPopularAuthors(authorArray, booksArray, total) {
return authorArray.map(a => {
a.count = booksArray.filter(b => b.authorId === a.id).reduce((b, a) => b + (a.borrows && a.borrows.length || 0), 0);
a.name = `${a.name.first} ${a.name.last}`;
delete a.id;
return a;
}).sort((a, b) => b.count - a.count).slice(0, total)
}

console.log(getPopularAuthors(authors, books, 5))

关于javascript - 如何按流行度从数组中获取前五个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68474133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com