4

I`m trying to write a loop that will read through a nested number array.

The JSON file that I`m reading goes like this. each number key represents event dates. json reference for startdate and end date enter image description here

I have below javascript code that reads per var i = 1 or j = 1. I`d like to read through entire nested number from dates and store them somewhere.

$(document).ready(function () {
  $.getJSON('http://app.toronto.ca/cc_sr_v1_app/data/edc_eventcal_APR?limit=500', function (data) {
    var data = data;
    var i = 2;
    var obj = data[i].calEvent;  
    var bingname = obj.eventName;
    var j = 1;
    var startdate = obj.dates[j].startDateTime;
    var time = new Date(startdate);
    var starttime = time.getFullYear()+'-' + (time.getMonth()+1) + '-'+time.getDate();
    var name = JSON.stringify(bingname);   

    document.getElementById("bingname").innerHTML = name;


    document.getElementById("bingtime").innerHTML = starttime;


    var name = firebase.database().ref("/bing").set({
      EventName : name,
      EventStart : starttime
    });

  });
});

Now, I should use something of incremental loop for var j. But I'm not sure how. The problem for me is that json retrieved in obj.dates[j] doesn't seem like an array. I can't seem to read it as list of numbers to read through. Help is much appreciated.

If anyone can even sort this nearest to furthest from today's date that'd be Einstein:)

1
  • obj.dates[j] is an object. You can iterate the array with a for loop. These are both really basic javascript concepts that you should learn.
    – James
    Commented Jul 20, 2017 at 19:25

2 Answers 2

1

You will get an array of objects, that includes a callEvent object that has a dates property which is an array with objects with the property's startDateTime and endDateTime. It will look like following:

[
    {
      callEvent: {
        dates: [
          {startDateTime: '', endDateTime: ''},
          // more objects with start- and endDateTime
        ]
      }
    },
    // more callEvent objects..
]

Now your code should loop through the array to get all callEvent objects and loop through all dates objects inside each callEvent.

$(document).ready(function () {
    $.getJSON('http://app.toronto.ca/cc_sr_v1_app/data/edc_eventcal_APR?limit=500', function (array) {

        // loop through all elements in the array
        for (var i = 0; i < array.length; i++) {
            // loop through all dates inside the array 
            for (var j = 0; j < array[i].calEvent.dates.length; j++) {
                console.log(array[i].callEvent.dates[j].startDateTime)
                console.log(array[i].callEvent.dates[j].endDateTime)
            }
        }

    });
});
1
  • Shouldn't data[i] be array[i] and array[i].dates be data[i].callEvent.dates?
    – RobG
    Commented Jul 20, 2017 at 21:02
0

Assuming the dates are valid JSON (JSON Validator), then you should just be able to get the data and loop through it:

for (var i=0;i<data.length;++i) {
  console.log(data[i].startDateTime);
  console.log(data[i].endDateTime);
}
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.