0

I am preparing a chart in Javascript. I have an array element named groupedByWeek this groupedweek is derived from JSON data. Now i am having it in form of child arrays like this: groupedByWeek = Object { 1: Array[4], 2: Array[7], 3: Array[3] }

screenshot of console is here: enter image description here

Now i want to parse each of groupedByWeek elements for graph, for the following code:

function increment(){
    var i = groupedByWeek[1];
    barChart1.parse(i,"json");
    for (; i <= groupedByWeek.length; i++){
        barChart1.parse(i,"json");
    }
}

and

<input type="button" onClick="increment()" value="Next"/>

but this is not working! Infact nothing is working out inside for loop while i am doing console.log()

If i am doing like this then it's working for 1st elemnt only!!:

var i = groupedByWeek[1];
barChart1.parse(i,"json");

please give me a hint how to work it out!

2
  • 1
    Couple of problems here. var i = [array] is the same i used in your for loop?? That would be expecting a number, not an array object. Also you misspelled length. Commented Oct 15, 2014 at 17:13
  • crrected spelling mistake. thanks! yes 'i' is same though! this is just for refrence Commented Oct 15, 2014 at 17:32

1 Answer 1

1

You have several problems:

  • Your array is containing an object, not an index.
  • You spelt length as legnth.

You should be attempting to iterate through your object, rather than an Array. So you would do something along these lines:

Javascript:

for(var index in object) {
    // Do something.
}

jQuery:

$.each(object, function(index, value) {
    // Do something.
});

I believe that was your original goal, iterate your object not a collection of the items.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks greg for your approach! let me check and let you know asap!
can you please guide me to iterate each groupedByWeek object incrementally and assign each of them to separate variable 'i'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.