0

I have a script that console logs the following:

 console.log(this.props.comments);
 console.log(Object.keys(this.props.comments)); //why length 0???

The output:

enter image description here

If this.props.comments has a child node and that it console.logs correctly, why does Object.keys console log an empty array? This is really stumping me. I use Object.keys quite a lot and I have never come across this before. I am expecting the second console.log to output: ["AFIFAIFJJ"]

I am hoping that I am doing something rather stupid! All help is appreciated. Thanks in advance.

p.s. I am using React-Native although, the question is just regarding Javascript I believe.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
HarryShotta
  • 347
  • 1
  • 15
  • It gets populated asynchronously. – CertainPerformance Sep 12 '19 at 11:30
  • Sorry, I am not sure how this works. From the link above, it seems like the issue is how the logs are populated in chrome. My issue is that the array is empty (hence I tried to console log it and hence the console log came out empty). Just in case I misunderstood, I tried console.log(Object.keys(this.props.comments)[0]); and console.log(Object.keys(this.props.comments)["ANANFFAF"] which both returned undefined); Have I misunderstood something? – HarryShotta Sep 12 '19 at 11:44
  • Essentially, I am trying to run the following, but scrollables is an empty array. For some reason Object.keys() is returning an empty array: let scrollables =Object.keys(this.props.comments).map((com)=>{ return ()} ); – HarryShotta Sep 12 '19 at 11:50
  • Yep, that's as expected. The `.comments` object gets populated asynchronously, so if you examine the *object itself* with `console.log`, you'll see *later* that it has stuff in it, even though during runtime, at the point you log it, it's actually empty. You need to wait for the asynchronous action to complete somehow. – CertainPerformance Sep 12 '19 at 11:51
  • Now I'm 10000% confused XD. Ignoring the logs for a moment as I only added the logs in to debug it. I am rendering scrollables to the screen and nothing renders. I am pretty sure that this is because scrollables =[]; – HarryShotta Sep 12 '19 at 11:56
  • You have something asynchronous going on that you need to sort out – CertainPerformance Sep 12 '19 at 11:57
  • Thanks for your help - think I've learnt something today :). – HarryShotta Sep 12 '19 at 12:16

0 Answers0