Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been looking all over stackoverflow for something to help me implement an AreaChart. The problem is that I have something like this.

var items = new Array();
if ($(result).find('dato').length > 0 ) {
  items = ["Date", "Kg Emitted", "Kg Reduced"];
  $(result).find('dato').each(
    function (i) {
      var item = new Array();
      var date = $(this).find("fecha").first().text();
      var kge = parseInt($(this).find("emitido").first().text());
      var kgr = parseInt($(this).find("reducido").first().text());
      item = [date,kge,kgr];
      items.push.apply(items, item)
    }
  );
};

The problem is that I need it in a format like:

items = ["Date","Kg Emitted", "Kg reduced"], [2013-01-01, 3, 4], [2013-01-02, 1, 3], etc

I would appreciate any help on how to format this nested array, because so far I've tried items.push.apply(items, item), but it doesn't seem to work.

share|improve this question
 
I forgot to mention. This each method is getting an ajax with an xml formated var that contains fecha, emitido and reducido, which is date, Kg Emitted and Kg reduced –  Joaquin Jun 11 at 14:47
add comment

3 Answers

up vote 1 down vote accepted

Everybody gave you the right answer here, i want to add just a little fix, the first value in your array is incorrect and is actually 3 rows.

See the difference in this fiddle

you need to add another [..] .

share|improve this answer
 
I did everything you guys told me, But the graph wasn't working, so I put an alert(items[0] + " " + items[1]+ " " + items[2]); And what I got was Date,Kg Emitted,Kg Reduced Date,Kg Emitted,Kg Reduced,,04-06-2013,1,0,,05-06-2013,8,0,,06-06-2013,1,0,,07-06-2013,21,0,,08-06-‌​2013,1,0,,09-06-2013,20,0,,10-06-2013,1,0 04-06-2013,1,0 ....why is repeating data? –  Joaquin Jun 11 at 15:15
 
I just realized that I had items.push(items,item); I'm testing to see if that was it –  Joaquin Jun 11 at 15:18
add comment

Just change items.push.apply(items, item) to items.push(item).

When you use apply like that you are effectively doing the same thing as Array.prototype.concat.

share|improve this answer
 
Thank you. It worked perfectly :) –  Joaquin Jun 11 at 15:20
add comment

Your initialization of items isn't setting it up as a nested array. Element 0 is "Date", 1 is "Kg Emitted" and 2 is "Kg Reduced".

You want to start it with

items = [["Date", "Kg Emitted", "Kg Reduced"]];

This will instead make Element 0 of the array be ["Date", "Kg Emitted", "Kg Reduced"], which is what you say you want.

Then, as others have already said, change items.push.apply(items, item) to

items.push(item);
share|improve this answer
1  
Thank you. It worked perfectly :) –  Joaquin Jun 11 at 15:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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