Robert Ross Robert Ross - 3 months ago 26
Ajax Question

forEach is not a function error

I get this error when I am trying to iterate through my JSON object with a foreach.
Can someone help, please?

Here is my JS:

function dateTimeChecker() {
"url": 'get-booked.php',
"method": "get",
"dataType": "text",
"cache": false
}).done(function(jBooked) {
var jBookedDates=JSON.parse(jBooked);
if (jB=="11/01/2016") {console.log("works");}else{console.log("doesn't");}

And here is the object in question :

enter image description here

Also, I am wondering how can i iterate over this object if someone cares to explain. :)


The response that you are receiving is a JSON. You can't use an array object's method forEach over a plain object. You have to use Object.keys() at this context to retrieve the enumerable own properties belongs to the parsed JSON,

 if (jB=="11/01/2016") {
 } else {

For your query in the comment, you can use bracket notation to access those arrays,

  var arr = jBookedDates[jB];
  console.log(arr); //will print the array belongs to each property.