How to get "json.forEach" to work in Code Pen

I’m doing the first Data Visualization project, and I’m just trying to get basic things to function before tackling the main problem.

To start I’ve copied over the of the exercises into the Code Pen below (with a new API), but whenever I click the button I just get “Uncaught TypeError: json.forEach is not a function.”

I’ve already added d3 libraries, and a bunch of json ones, what am I missing to get “forEach” to work?

The value of json is not an array, so it doesn’t have the forEach method available. Try logging json to see what’s inside.

1 Like

Hey! You can use the JSON.parse() method to convert it first and then if the thing you parsed is of type array then you should be able to iterate over it with the forEach method Since it can be only be used to iterate over arrays.
Hope this helped! :smiley:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.