returns 对象命名的函数
function that returns object names
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],
},
};
我需要创建一个函数来执行此操作:此函数 returns 所有访问过给定用户愿望清单上任何公园的用户名。
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));
}
请记住:我刚刚完成了课程的一部分,其中涵盖了高级功能(查找、过滤、映射、某些、每个、forEach),我应该使用它们来解决问题。
让我们回顾一下您的尝试:
let wish = userId.wishlist;
这里,userId
是一个字符串。一个字符串没有属性wishlist
。您需要获取该 ID 对应的用户对象:users[userId].wishlist
。就像你在第二行所做的那样:
users[userId].visited.map((visited) => wish.includes(visited));
但是,map
is not the best method for what you want to achieve. You want to filter
the user names to keep only the ones that pass a condition. Which is that some
parks in the wishlist are included
在该用户的已访问公园列表中:
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] }
};
function getUsersForUserWishlist(users, userId) {
const wishlist = users[userId].wishlist;
// Get all user names
return Object.keys(users)
// Filter them
.filter(
// If the wishlist has some elements which that user has visited
name => wishlist.some(park => users[name].visited.includes(park))
);
}
console.log(getUsersForUserWishlist(users, "karah.branch3")); //> ["don.kim1990"]
console.log(getUsersForUserWishlist(users, "dwayne.m55")); //> []
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],
},
};
我需要创建一个函数来执行此操作:此函数 returns 所有访问过给定用户愿望清单上任何公园的用户名。
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));
}
请记住:我刚刚完成了课程的一部分,其中涵盖了高级功能(查找、过滤、映射、某些、每个、forEach),我应该使用它们来解决问题。
让我们回顾一下您的尝试:
let wish = userId.wishlist;
这里,userId
是一个字符串。一个字符串没有属性wishlist
。您需要获取该 ID 对应的用户对象:users[userId].wishlist
。就像你在第二行所做的那样:
users[userId].visited.map((visited) => wish.includes(visited));
但是,map
is not the best method for what you want to achieve. You want to filter
the user names to keep only the ones that pass a condition. Which is that some
parks in the wishlist are included
在该用户的已访问公园列表中:
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] }
};
function getUsersForUserWishlist(users, userId) {
const wishlist = users[userId].wishlist;
// Get all user names
return Object.keys(users)
// Filter them
.filter(
// If the wishlist has some elements which that user has visited
name => wishlist.some(park => users[name].visited.includes(park))
);
}
console.log(getUsersForUserWishlist(users, "karah.branch3")); //> ["don.kim1990"]
console.log(getUsersForUserWishlist(users, "dwayne.m55")); //> []