Return 一个对象值作为另一个对象值出现的次数的数字
Return a number for the amount of times an object value appears as another object value
帐户数组中的帐户对象示例:
const accounts = [
{
id: "5f446f2ecfaf0310387c9603",
picture: "https://api.adorable.io/avatars/75/esther.tucker@zillacon.me",
age: 25,
name: {
first: "Esther",
last: "Tucker",
},
company: "ZILLACON",
email: "esther.tucker@zillacon.me",
registered: "Thursday, May 28, 2015 2:51 PM",
},
书籍数组中的书籍对象示例:
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,
},
],
},
我需要我的函数 return 一个 数字 表示帐户 ID 在任何书籍的 borrow
数组中出现的次数。
这是我拥有的:
function getTotalNumberOfBorrows(account, books) {
const accId = account.id;
let idBorrowed = books.filter((book) => accId === book.borrows.id);
return idBorrowed.length;
}
我在考试中应该得 2 分,却得了 0 分。请注意,我刚刚学习完高级功能,预计会在需要时使用查找、过滤、映射、缩减和解构对象。感谢任何 help/advice.
borrows
是一个数组,需要遍历它才能得到每个借用的id
属性。
function getTotalNumberOfBorrows(account, books) {
const accId = account.id;
let total = 0;
books.forEach(book => book.borrows.forEach(borrow => accId === borrow.id && total++));
return total;
}
如果您需要使用“高级”功能,另一种可能的方法
function getTotalNumberOfBorrows(account, books) {
const { id: accId } = account;
return books.reduce((accumulator, book) => {
return (
accumulator +
book.borrows
.filter(borrow => borrow.id === accId)
.reduce((accumulatorBorrows, borrow) => accumulatorBorrows + 1, 0)
);
}, 0);
}
帐户数组中的帐户对象示例:
const accounts = [
{
id: "5f446f2ecfaf0310387c9603",
picture: "https://api.adorable.io/avatars/75/esther.tucker@zillacon.me",
age: 25,
name: {
first: "Esther",
last: "Tucker",
},
company: "ZILLACON",
email: "esther.tucker@zillacon.me",
registered: "Thursday, May 28, 2015 2:51 PM",
},
书籍数组中的书籍对象示例:
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,
},
],
},
我需要我的函数 return 一个 数字 表示帐户 ID 在任何书籍的 borrow
数组中出现的次数。
这是我拥有的:
function getTotalNumberOfBorrows(account, books) {
const accId = account.id;
let idBorrowed = books.filter((book) => accId === book.borrows.id);
return idBorrowed.length;
}
我在考试中应该得 2 分,却得了 0 分。请注意,我刚刚学习完高级功能,预计会在需要时使用查找、过滤、映射、缩减和解构对象。感谢任何 help/advice.
borrows
是一个数组,需要遍历它才能得到每个借用的id
属性。
function getTotalNumberOfBorrows(account, books) {
const accId = account.id;
let total = 0;
books.forEach(book => book.borrows.forEach(borrow => accId === borrow.id && total++));
return total;
}
如果您需要使用“高级”功能,另一种可能的方法
function getTotalNumberOfBorrows(account, books) {
const { id: accId } = account;
return books.reduce((accumulator, book) => {
return (
accumulator +
book.borrows
.filter(borrow => borrow.id === accId)
.reduce((accumulatorBorrows, borrow) => accumulatorBorrows + 1, 0)
);
}, 0);
}