最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create nested arrays? - Stack Overflow

programmeradmin1浏览0评论

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.

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 edited Oct 14, 2018 at 4:15 Dave Newton 160k27 gold badges260 silver badges308 bronze badges asked Jun 11, 2013 at 14:44 JoaquinJoaquin 431 gold badge1 silver badge5 bronze badges 1
  • 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 Commented Jun 11, 2013 at 14:47
Add a ment  | 

3 Answers 3

Reset to default 1

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 [..] .

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.

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);
发布评论

评论列表(0)

  1. 暂无评论