0
[
    {
        "name":"A",
        "month":"Apr",
        "value":1234
    },
    {
        "name":"A",
        "month":"May",
        "value":2345
    },

    {
        "name":"A",
        "month":"Jun",
        "value":3457
    },
    {
        "name":"A",
        "month":"Jul",
        "value":6754
    }
]

I want to group the above JSON structure like this using JavaScript or jQuery:

   [{
     "id":"A",
     "data":[
     ["Apr",1234],
     ["May",2345],
     ["Jun",3457],
     ["Jul",6754],
     ]
   }]
4
  • 4
    How about some for and if? Commented Jan 22, 2015 at 6:59
  • It is a matter of a loop. Commented Jan 22, 2015 at 7:04
  • jsfiddle.net/1gxnxo2b here is the solution as said above using some for and if loops Commented Jan 22, 2015 at 8:06
  • jsfiddle.net/g0j1cL2m let me know if you want any changes. Commented Jan 22, 2015 at 9:02

1 Answer 1

0

Hi how about using underscore.js for this, its pretty simple with that You can group by with underscore and then use the final json with your HTML Template.

var groupedData = _.groupBy(data, function(d){return d.name});

The live example here DEMO

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

2 Comments

i have tried your method....but it is not working.....i am using the json data given above...
jsfiddle.net/1gxnxo2b here is the solution as said above using some for and if loops

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.