如何将数组与高阶函数进行比较? JavaScript

How to compare arrays with higher order functions? JavaScript

我正在尝试将数组与 属性 进行赋值比较,我只能使用高阶函数(.map()、.filter()、.forEach()、.some( ) ETC...)。该数组是“userWishlist”,属性来自“parks”数组“parks[index].id”中对象的 ID。有没有人对解决此类问题有任何建议?我的代码目前只有 returns:

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

调用时:

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

为什么我的代码 return 不能匹配“wishlist”数组的所有“park”对象?

/* 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"));

问题是 index 是当前心愿单元素的索引,但您将其用作 parks 的索引。因此,如果公园在 parks 数组中的索引与其在 wishlist.

中的 ID 相同,那么您只能 return 一个公园

您需要使用 find() 来查找具有当前心愿单 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"));

你的代码有两个问题,

  1. const findUser = Object.keys(users).filter(user => user == userID); 不需要这样做,因为您可以使用 user[userId] 来获取特定用户,因为用户是一个像地图一样的对象。

  2. const selectedItem = parks.find(item => item.id === wish) 您需要另一种搜索方法来为您找到正确的 parkitem,仅获取与 userWishlist 具有相同索引的部分项目将无法解决问题。

我已经修改了你的代码,请试试这个

/* 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"));

这里可以直接使用用户whishList

的地图来解决

对于你的每一个愿望,你都映射到公园搜索的对象

/* 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"));