0

I'm trying to figure out the best way return an object found as a key's value, based upon the given unique key...

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"
      }
    ]
  }
];

With this code I get the following error postOwner is not defined. I'm guessing this is because the postOwner: userAccounts.filter(...) call is returning an array with the object as an element.

Instead... I just want to return the object itself, that was found from the entered uniqueKey.

Any idea how to do this?

Edit: made changes in code to match suggestions given in comments below, but still receiving the same error...

Fiddle Freak
  • 1,923
  • 5
  • 43
  • 83
  • That's not how JavaScript and objects work. – Andreas Nov 20 '18 at 18:14
  • Why you're using `.filter()` at all? With the given example `userAccounts.filter(user => user.uniqueKey === x) === userAccounts[x - 1]` – Andreas Nov 20 '18 at 18:15
  • Both of the linked questions relate to what you're trying to do. Yes, `filter` returns an array; to just get the (first) match, use `find` (that's [the first question](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects)). You can't refer to the `postOwner` property on the object in a subsequent property initializer on the same object (that's [the second question](https://stackoverflow.com/questions/4616202/self-references-in-object-literals-initializers)). *(cont'd)* – T.J. Crowder Nov 20 '18 at 18:18
  • *(continuing)* ...so you'll need to have a `postOwner` variable prior to `userPosts` which is the result of finding the owner, and then use that in your `userPosts` initializer. – T.J. Crowder Nov 20 '18 at 18:20
  • I've tried the following on sandbox, but am not getting a console log of the username... https://codepen.io/jamespagedev/pen/yQpVRv?editors=0011 – Fiddle Freak Nov 20 '18 at 18:26
  • I made the changes in the code in my question to match the suggestions on using `.find()` and assigning it to a local variable first before assigning it as a value in the key.... but I'm still receiving the same error. Any idea why this is? – Fiddle Freak Nov 20 '18 at 18:42

0 Answers0