Skip to content
Advertisement

Identifying element in multidimensional javascript object

I’m playing with a netflix dataset in p5.js and am having issues viewing individual elements (purely for debugging purposes, for now).

E.g I’m trying to view element 0 in the array/object(?) but it always returns undefined.

For example, if I console.log the array/object itself, I can see entries:

JavaScript

However, if I try and print [0] for my array, it shows as undefined

JavaScript

But if I try what I usually would with a multidimensional array, I see the following, which implies my type is off, but typeOf only shows it as an ‘object’

JavaScript

This seems to imply that this isn’t an array at all. What is it?

Any help or guidance is appreciated. Thanks!

For reference, this is how the object was originally created (full 8000~ dataset was condensed via this method: https://www.tutorialspoint.com/counting-unique-elements-in-an-array-in-javascript

Advertisement

Answer

myArray is an object. For an object myArray[0] doesn’t work. Fetching an element with index only works for Array.

You can use the below code to loop over all the key-values.

JavaScript

If you want the list of keys then you can use Object.keys

JavaScript

And if you want the list of values then you can use Object.values

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement