通过标识其关键值来获取非特定对象

时间:2018-11-20 18:11:28

标签: javascript

我正在尝试根据给定的唯一键,找出返回作为键值的对象的最佳方法...

export const userAccounts = [
  {
    uniqueKey: 1,
    username: 'Guest',
    password: 'guest',
    thumbnailUrl: 'default'
  },
  {
    uniqueKey: 2,
    username: 'philzcoffee',
    password: 'hotCup123',
    thumbnailUrl: 'https//myImages.com/myImagephilzcoffee.jpg'
  },
  {
    uniqueKey: 3,
    username: 'fortnite',
    password: 'shooter234',
    thumbnailUrl: 'https//myImages.com/myImageFortnite.jpg'
  },
  {
    uniqueKey: 4,
    username: 'playhearthstone',
    password: 'cards345',
    thumbnailUrl: 'https//myImages.com/myImagePlayhearthstone.jpg'
  },
  {
    uniqueKey: 5,
    username: 'George Costanza',
    password: 'creator',
    thumbnailUrl: 'default'
  },
  {
    uniqueKey: 6,
    username: 'biancasaurus',
    password: 'pass1',
    thumbnailUrl: 'default'
  },
  {
    uniqueKey: 7,
    username: 'martinseludo',
    password: 'pass2',
    thumbnailUrl: 'default'
  },
  {
    uniqueKey: 8,
    username: 'michaelmarzetta',
    password: 'pass3',
    thumbnailUrl: 'default'
  },
  {
    uniqueKey: 9,
    username: 'themexican_leprechaun',
    password: 'pass4',
    thumbnailUrl: 'default'
  },
  {
    uniqueKey: 10,
    username: 'dennis_futbol',
    password: 'pass5',
    thumbnailUrl: 'default'
  },
  {
    uniqueKey: 11,
    username: 'awaywetravel',
    password: 'pass6',
    thumbnailUrl: 'default'
  },
  {
    uniqueKey: 12,
    username: 'awesomebt28',
    password: 'pass7',
    thumbnailUrl: 'default'
  },
];

let philzcoffee = userAccounts.find(user => user.uniqueKey === 2);
let fortnite = userAccounts.find(user => user.uniqueKey === 3);
let playhearthstone = userAccounts.find(user => user.uniqueKey === 4);
export const userPosts = [
  {
    postOwner: philzcoffee,
    username: postOwner.username,
    password: postOwner.password,
    thumbnailUrl: postOwner.thumbnailUrl,
    imageUrl:
      "https://tk-assets.lambdaschool.com/69cf901b-f96d-466e-a745-ff2a01effac9_philz-image.jpg",
    likes: 400,
    timestamp: "July 17th 2017, 12:42:40 pm",
    comments: [
      {
        username: "philzcoffee",
        timestamp: "July 17th 2017, 12:42:40 pm",
        text:
          "We've got more than just delicious coffees to offer at our shops!"
      },
      {
        username: "biancasaurus",
        timestamp: "July 17th 2017, 1:00:32 pm",
        text: "Looks delicious!"
      },
      {
        username: "martinseludo",
        timestamp: "July 17th 2017, 1:02:45 pm",
        text: "Can't wait to try it!"
      }
    ]
  },
  {
    postOwner: fortnite,
    username: postOwner.username,
    password: postOwner.password,
    thumbnailUrl: postOwner.thumbnailUrl,
    imageUrl:
      "https://tk-assets.lambdaschool.com/89d13918-b7a2-4b40-9658-f376ea3f6b59_37131538_213683546146400_1083714364399157248_n.jpg",
    likes: 4307,
    timestamp: "July 15th 2017, 03:12:09 pm",
    comments: [
      {
        username: "twitch",
        timestamp: "July 15th 2017, 03:12:21 pm",
        text: "Epic Street Fighter action here in Las Vegas at #EVO2017!"
      },
      {
        username: "michaelmarzetta",
        timestamp: "July 15th 2017, 03:14:09 pm",
        text: "Omg that match was crazy"
      },
      {
        username: "themexican_leprechaun",
        timestamp: "July 15th 2017, 03:32:52 pm",
        text: "What a setup"
      },
      {
        username: "dennis_futbol",
        timestamp: "July 15th 2017, 04:23:44 pm",
        text: "It that injustice"
      },
      {
        username: "dennis_futbol",
        timestamp: "July 15th 2017, 04:23:55 pm",
        text: "Is"
      }
    ]
  },
  {
    postOwner: playhearthstone,
    username: postOwner.username,
    password: postOwner.password,
    thumbnailUrl: postOwner.thumbnailUrl,
    imageUrl:
      "https://tk-assets.lambdaschool.com/43bf01f9-319c-469d-8cf5-0120fe1007f1_yosemite.jpg",
    likes: 5306,
    timestamp: "July 14th 2017, 10:04:08 am",
    comments: [
      {
        username: "playhearthstone",
        timestamp: "July 14th 2017, 10:04:08 am",
        text: "Love this shot!"
      },
      {
        username: "awaywetravel",
        timestamp: "July 15th 2017, 11:52:13 am",
        text: "Yosemite is my most favorite place in the universe"
      },
      {
        username: "awesomebt28",
        timestamp: "July 15th 2017, 12:16:31 pm",
        text: "I like how Half Dome looks so old and useless"
      }
    ]
  }
];

使用此代码,我得到以下错误postOwner is not defined.我猜这是因为postOwner: userAccounts.filter(...)调用正在返回一个以对象为元素的数组。

相反,我只想返回从输入的uniqueKey中找到的对象本身。

有什么想法吗?

编辑:对代码进行了更改以匹配以下注释中给出的建议,但仍然收到相同的错误...

0 个答案:

没有答案