简体   繁体   中英

How to compare arrays with higher order functions? JavaScript

I'm trying to compare an array with a property for an assignment and I can only use higher order functions (.map(), .filter(), .forEach(), .some() etc...). The array is "userWishlist" and the properties are from the ID's in objects from the "parks" array "parks[index].id". Does anyone have any advice for solving this sort of problem? My code as of right now only returns:

[{"id":1,"name":"Acadia","areaInSquareKm":198.6,"location":{"state":"Maine"}}]

when called with:

getWishlistParksForUser(parks, users, "dwayne.m55")

Why won't my code return all of the "park" objects that match the "wishlist" array?

 /* Function should compare the user's wishlist with the parks */ function getWishlistParksForUser(parks, users, userID) { let wishArray = []; // Find the user const findUser = Object.keys(users).filter(user => user == userID); // Create array out of wishlist const userWishlist = users[findUser].wishlist.map(list => list); // Compare the two arrays and push park into wishlist array const parkID = userWishlist.forEach((wish, index) => { if (wish === parks[index].id) wishArray.push(parks[index]) }) return wishArray; } // The users objects (non-editable) const users = { "karah.branch3": { visited: [2], wishlist: [1, 3], }, "dwayne.m55": { visited: [2, 3], wishlist: [1, 3], }, }; // The parks array (non-editable) 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: "Zion", areaInSquareKm: 595.9, location: { state: "Utah" }, }, ]; console.log(getWishlistParksForUser(parks, users, "dwayne.m55"));

The problem is that index is the index of the current wishlist element, but you're using it as an index into parks . So you only return a park if it's at the same index in the parks array as its ID in the wishlist .

You need to use find() to find the park with the current wishlist ID.

 /* Function should compare the user's wishlist with the parks */ function getWishlistParksForUser(parks, users, userID) { let wishArray = []; const userWishlist = users[userID].wishlist; // Compare the two arrays and push park into wishlist array userWishlist.forEach((wish) => { let foundPark = parks.find(p => p.id == wish); if (foundPark) { wishArray.push(foundPark) } }) return wishArray; } // The users objects (non-editable) const users = { "karah.branch3": { visited: [2], wishlist: [1, 3], }, "dwayne.m55": { visited: [2, 3], wishlist: [1, 3], }, }; // The parks array (non-editable) 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: "Zion", areaInSquareKm: 595.9, location: { state: "Utah" }, }, ]; console.log(getWishlistParksForUser(parks, users, "dwayne.m55"));

there're two problems in your code,

  1. const findUser = Object.keys(users).filter(user => user == userID); no need to do this,as you could just use user[userId] to get specific user as user is an object like a map.

  2. const selectedItem = parks.find(item => item.id === wish) you need another search method to get right parkitem for you,just get part item with same index of userWishlist will not do the trick.

i have modified your code, please try this

/* Function should compare the user's wishlist with the parks 

*/
function getWishlistParksForUser(parks, users, userID) {
  let wishArray = [];
  // Find the user
//   const findUser = Object.keys(users).filter(user => user == userID);
  // Create array out of wishlist
  const userWishlist = users[userID].wishlist.map(list => list);
  // Compare the two arrays and push park into wishlist array
  const parkID = userWishlist.forEach((wish, index) => {
    const selectedItem = parks.find(item => item.id === wish)
    if (selectedItem) wishArray.push(selectedItem)
  })

  return wishArray;
}

// The users objects (non-editable)
const users = {
  "karah.branch3": {
    visited: [2],
    wishlist: [1, 3],
  },
  "dwayne.m55": {
    visited: [2, 3],
    wishlist: [1, 3],
  },
};

// The parks array (non-editable)
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: "Zion",
    areaInSquareKm: 595.9,
    location: {
      state: "Utah"
    },
  },
];

console.log(getWishlistParksForUser(parks, users, "dwayne.m55"));

Here's you can solve it using directly a map from the user whishList

for each whish Id you map to the object searching on the park

 /* Function should compare the user's wishlist with the parks */ function getWishlistParksForUser(parks, users, userID) { return users[userID].wishlist.map(wishId => parks.find(p => p.id == wishId)); } // The users objects (non-editable) const users = { "karah.branch3": { visited: [2], wishlist: [1, 3], }, "dwayne.m55": { visited: [2, 3], wishlist: [1, 2], }, }; // The parks array (non-editable) 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: "Zion", areaInSquareKm: 595.9, location: { state: "Utah" }, }, ]; console.log(getWishlistParksForUser(parks, users, "dwayne.m55"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM