![](/img/trans.png)
[英]compare array of object of array with an another array in javascript
[英]Trying to compare an array with another array nested in an object
我在這個問題上已經被困了一段時間。 如果有人對我如何 go 有任何指導。
此 function 返回訪問給定用戶願望清單中任何公園的所有用戶名。
getUsersForUserWishlist(users, "karah.branch3"); //> ["dwayne.m55"]
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], }, }; function getUsersVisitedForUserWishlist(users, username) {}
如果我正確理解了您的 function 必須返回(如下例所示)而不是您的示例中提到的內容:
getUsersForUserWishlist(users, "karah.branch3"); //> ["don.kim1990"]
getUsersForUserWishlist(users, "dwayne.m55"); //> []
這解決了你的問題
function getUsersForUserWishlist(users1, userName) {
//Retreive the user wishlist
var wishlistInd = users1[arguments[1]]["wishlist"];
var arrayRes = [];
//loop throght users array
for (const [key, value] of Object.entries(users1)) {
var filteredArray = [];
if ( `${key}` !== userName ) {
var filteredArray = value['visited'].filter(value =>
wishlistInd.includes(value));
if (filteredArray.length != 0 ) {
arrayRes.push( `${key}`);
}
}
}
return arrayRes;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.