0

I am having an issue in accessing the object through key which got set dynamically with in a loop.

Setting Data in an object as follows:

let carddata = await cardresult.json(); // es6 promise data 
memberDataObject[item.id] = {
   cards: carddata,
   boardName: item.name
}

Following is my Console Log of memberDataObject Console Log of MemberDataObject

I am trying to access data as follows:

console.log(MemberDataObject['5a8a9d6305e9658367740bb7']);

What I want: I want to access the data by key in a loop which is an id of other data array. somthing like this :

memberBoards.boards.map( (boarditem,key) => {
   let data = MemberDataObject[boarditem.id];
   console.log(data);
})

But console.log(data) is giving undefined in console.log but instead I expect the array to be printed.

Notes: I am running this code in ReactJS, following ES6 with EsLint and Babel.

I have googled around and found other answers in StackOverflow but nothing works. I might be missing something very minor.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Gitesh Dang
  • 182
  • 1
  • 14
  • You log the object when it is not yet filled (`{}`) and them accessing props results in undefined. what you see in the unfolded box is a live view of the object, which means that the data was added later. You have to wait for that later point before accessing the data. – Jonas Wilms Aug 28 '18 at 17:19
  • 1
    Is that `memberBoards` line inside the `async` function? – MinusFour Aug 28 '18 at 17:21
  • @JonasWilms Yes I am trying to access that data after it is loaded. As you can see in the screenshot I have attached - the data is loaded into an object. I have used the above code at the same position after `memberDataObject` get consoled – Gitesh Dang Aug 28 '18 at 17:25
  • @MinusFour Yes I have tried using async as well but no luck. Have been playing around 2 days with this. Changed the whole way of data i.e sometimes to array sometimes to object and sometimes a mix of array n object but no luck. – Gitesh Dang Aug 28 '18 at 17:26
  • @MinusFour I am getting undefinied already by putting the key manually. I would like to get that solved first so that I can use loop of memberBoards. – Gitesh Dang Aug 28 '18 at 17:28
  • `As you can see in the screenshot ` ... the data is *not* loaded yet. the chrome console is a bit missleading, the thing that actually gets logged is the `{}` (an empty object) – Jonas Wilms Aug 28 '18 at 17:36
  • @JonasWilms I see. What could be the issue? Am I not making the object correctly? as this : `memberDataObject[item.id] = { cards: carddata, boardName: item.name }` – Gitesh Dang Aug 28 '18 at 17:41
  • no thats fine. you just execute the code in the wrong order. (hard to tell without the whole code) – Jonas Wilms Aug 28 '18 at 17:41
  • @JonasWilms How can I show the whole code? In Screenshots or should I put in my question? – Gitesh Dang Aug 28 '18 at 17:43
  • have you ever tried to copy some text from a screenshot? ... – Jonas Wilms Aug 28 '18 at 17:52
  • @JonasWilms Just thought to describe the order of execution here : I am setting data object in a state of props using reducers and calling it in a component. However I am going to put the application in git because you can see the order of whole app. – Gitesh Dang Aug 28 '18 at 18:03

0 Answers0