user98239820
user98239820

Reputation: 1481

Javascript, restructuring array

I am trying to draw a google chart and I have a data from Api in array format. I am trying to restructure it to the format that google chart accepts.. I have been trying to manage but with no luck... its a problem of structuring loops in right way but i am stuck

{
      "rows": [
        [
          "20140803",
          "29917",
          "1"
        ],
        [
          "20140803",
          "30022",
          "1"
        ],
        [
          "20140804",
          "29917",
          "1"
        ],
        [
          "20140805",
          "29917",
          "3"
        ],
        [
          "20140805",
          "30022",
          "3"
        ],
        [
          "20140807",
          "29917",
          "6"
        ],
        [
          "20140807",
          "30022",
          "2"
        ]
      ]
    }

This is an array I receive.. here first value in each row is date, second is article id, third is number of views...
to create a graph i need array in the format of (just a example)

[date,     article_id1, article_id2, article_id3, article_id2,......]
[20140731,     12,         222,        0,       0]
[20140732,     333,         0,         0,       12]

Here the number of article id api gives in not fixed, also if I don't have data for that date to that article I would have to put 0..

More in brief.. Google Chart requires data in format such as

['Year', 'Sales', 'Expenses'],
['2004',  1000,      400],
['2005',  1170,      460],
['2006',  660,       1120],
['2007',  1030,      540]

So I am plotting graph with respect to each article id (like sale, expenses in example)

If any one could give me any heads up that would be really very helpful.. Thanks

Upvotes: 0

Views: 678

Answers (1)

2pha
2pha

Reputation: 10155

Here is one I came up with. Everything is done in the covert function. The rest is just for show.
http://jsfiddle.net/oxpasrnh/

It is probably not the best though, someone should be able to improve on it.

I don't want to post code, just look at the fiddle

Upvotes: 1

Related Questions