【问题标题】:How to compare arrays with higher order functions? JavaScript如何比较具有高阶函数的数组? JavaScript
【发布时间】:2020-12-04 02:46:17
【问题描述】:

我正在尝试将数组与分配的属性进行比较,我只能使用高阶函数(.map()、.filter()、.forEach()、.some() 等...) .数组是“userWishlist”,属性来自“parks”数组“parks[index].id”的对象中的 ID。有人对解决此类问题有什么建议吗?我的代码现在只返回:

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

调用时:

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

为什么我的代码不会返回与“愿望清单”数组匹配的所有“公园”对象?

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

【问题讨论】:

  • “从愿望清单中创建数组”。已经是数组了,为什么还要用map()做成数组呢?
  • findUser 只是一个包含userID 的数组。那有什么意义呢?为什么使用数组作为对象属性?只需使用users[userID].wishlist

标签: javascript arrays object properties higher-order-functions


【解决方案1】:

问题在于index 是当前愿望清单元素的索引,但您将它用作parks 的索引。所以你只返回一个公园,如果它在parks数组中的索引与它在wishlist中的ID相同。

您需要使用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"));

【讨论】:

    【解决方案2】:

    这里你可以直接使用来自用户 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"));

    【讨论】:

      【解决方案3】:

      你的代码有两个问题,

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

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

      【讨论】:

        猜你喜欢
        • 2021-11-01
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-06
        • 2020-03-11
        相关资源
        最近更新 更多