【发布时间】:2021-06-28 22:18:08
【问题描述】:
const parks = [
{
id: 1,
name: "Acadia",
areaInSquareKm: 198.6,
location: { state: "Maine" },
},
{
id: 2,
name: "Canyonlands",
areaInSquareKm: 1366.2,
location: { state: "Utah" },
},
{
id: 3,
name: "Crater Lake",
areaInSquareKm: 741.5,
location: { state: "Oregon" },
},
{
id: 4,
name: "Lake Clark",
areaInSquareKm: 10602,
location: { state: "Alaska" },
},
{
id: 5,
name: "Kenai Fjords",
areaInSquareKm: 2710,
location: { state: "Alaska" },
},
{
id: 6,
name: "Zion",
areaInSquareKm: 595.9,
location: { state: "Utah" },
},
];
const users = {
"karah.branch3": {
visited: [1],
wishlist: [4, 6],
},
"dwayne.m55": {
visited: [2, 5, 1],
wishlist: [],
},
thiagostrong1: {
visited: [5],
wishlist: [6, 3, 2],
},
"don.kim1990": {
visited: [2, 6],
wishlist: [1],
},
};
我需要创建一个函数来执行此操作:此函数返回访问过给定用户愿望清单中任何公园的所有用户名。
getUsersForUserWishlist(users, "karah.branch3"); //> ["dwayne.m55"]
getUsersForUserWishlist(users, "dwayne.m55"); //> []
这是我目前所拥有的,但它不起作用:
function getUsersForUserWishlist(users, userId) {
let wish = userId.wishlist;
return users[userId].visited.map((visited) => wish.includes(visited));
}
请记住:我刚刚完成了课程的一部分,涵盖了高级功能(find、filter、map、some、every、forEach),我应该使用它们来解决问题。
【问题讨论】:
-
我很难看到这里的逻辑。你能解释一下为什么
["dwayne.m55"]是karah.branch3的预期输出吗?根据您对函数“此函数返回访问给定用户愿望清单中任何公园的所有用户名”功能的描述,我希望它是["don.kim1990"],因为 dom.kim1990 访问了 Karah 愿望清单中的公园 6
标签: javascript arrays function javascript-objects higher-order-functions