0

I have an array which is printed as follows in the console. Not able to access its values, by

array[0]

but it has 4 elements. How to access these values and iterate?

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
usergs
  • 1,344
  • 3
  • 9
  • 18
  • 1
    what error do you see when you try to use array[0] ? – Bilal Alam Jul 17 '18 at 12:20
  • This question needs a [mcve] – Quentin Jul 17 '18 at 12:21
  • 1
    From the screenshot, we can tell that as of when you did `console.log`, the array was empty, but *later*, when you expanded that entry in the console, it had four entries. See the linked question and its answers for details. So you'll need to find out what event or similar you need to respond to that occurs when the array has had entries added to it (or post a [mcve] so we can help you figure it out). – T.J. Crowder Jul 17 '18 at 12:22
  • 1
    @LGSon — No. That shows it contains 4 things. There's nothing to suggest those things are arrays. We can see what they are: They are all numbers. – Quentin Jul 17 '18 at 12:23
  • @LGSon can you post how the data is var arr = [5,6,7] or var nestedArr = [ [8,9],[10,11],[12,13] ] – Learner Jul 17 '18 at 12:25
  • @Quentin And the `: Array []`, does ot refer to `array[0]` ? – Asons Jul 17 '18 at 12:27
  • @LGSon — No. The prototype (which is not spelt `0`) refers to the Array constructor function because the object is an array. – Quentin Jul 17 '18 at 12:27
  • @Quentin Ok... thanks – Asons Jul 17 '18 at 12:28
  • @Quentin I'm back :) ... if you run this https://jsfiddle.net/vubqn871/ in Firefox (which it appears OP did), it actually show exactly what type it is in the console, in all 3 cases, like it does in OP's image ` Array []` ... so am I still wrong? ...and it does that in Chrome too, though with `__proto__:Array(0)` – Asons Jul 17 '18 at 14:11
  • @LGSon — The OP said they could not access the elements of the array with `array[0]` not that they were logging `array[0]` – Quentin Jul 17 '18 at 14:16
  • @Quentin No, they asked _how_ to access them, not that they couldn't. With that it can't be a dupe of the one you linked to...but for sure there is another dupe :) – Asons Jul 17 '18 at 14:17
  • @LGSon — Quote: Not able to access its values, by array[0] – Quentin Jul 17 '18 at 14:24
  • @Quentin I know :) ... sorry, didn't mean you closed it as a dupe, got to excited in our discussion so it just pop'd out :) ... still, as you can see in the console, it actually tell what type it is, right? – Asons Jul 17 '18 at 14:29
  • @T.J.Crowder OP actually asked _"Not able to access its values, by `array[0]`, but it has 4 elements. How to access these values and iterate?"_ ... With that I think the dupe might not be the most appropriate, but for sure there is one iterate nested arrays :) .... added one to the link list – Asons Jul 17 '18 at 14:34
  • @LGSon - As Quentin said, there's no indication of a nested array above. *"Not able to access its values, by `array[0]`"* tells me they tried to access the value at `array[0]` and didn't get it, but when they logged the array, they seemed to have the value `2` there. – T.J. Crowder Jul 17 '18 at 15:25

0 Answers0