1

I want to iterate over a JSON in javasacript and create something like this as output

   {
    "seriesData": [
        {
            "name": "John",
            "data": [
                5,
                3,
                4
            ]
        },
        {
            "name": "Jane",
            "data": [
                2,
                2,
                3
            ]
        },
        {
            "name": "Joe",
            "data": [
                3,
                4,
                4
            ]
        }
    ]
}

So, I essentially need to add values in data array for each key inside my for loop.

Input looks like: {"Records":[{"name":"Jan'10","users":[{"name":"John","y":5},{"name":"Jane","y":2},{"name":"Joe","y":3}]},{"name":"Jan'10","users":[{"name":"John","y":3},{"name":"Jane","y":2},{"name":"Joe","y":4}]},{"name":"Jan'10","users":[{"name":"John","y":4},{"name":"Jane","y":3},{"name":"Joe","y":4}]}]};

Can someone please suggest how can this be achieved.

user1744413
  • 115
  • 1
  • 10
  • 2
    What exactly are you having problems with? Don't you know how to add values to an array? – Felix Kling May 04 '14 at 16:39
  • How to associate an array to a key in JSON ? And I need to keep track of all the arrays as I need to decide iteratively based on key which array to add new value to – user1744413 May 04 '14 at 16:40
  • I think you have to provide more information, especially how your original data looks like and how exactly you are trying to convert it. – Felix Kling May 04 '14 at 16:42
  • 2
    A lost cause, I know, but: that's not JSON. – JJJ May 04 '14 at 16:43
  • @Juhana please elaborate. – Robbert May 04 '14 at 16:46
  • @Robbert What the OP has is an array of objects. If he passed it to a JSON serializer it would be turned into a string that would be in a JSON format. JavaScript object != JSON. (edit: although the original input does look like JSON.) – JJJ May 04 '14 at 16:48
  • @Juhana the only difference being that the collection does not have a name? e.g. `{"collection":[{}, {}, {}]}` – Robbert May 04 '14 at 16:52
  • @Robbert No. JSON is a data transfer format (JS Object **Notation**). If you have *an actual object* then it's not a *notation*. See e.g. http://stackoverflow.com/a/383699/502381 – JJJ May 04 '14 at 16:54
  • @Juhana right, got it. Either way, OP didn't say it was JSON. – Robbert May 04 '14 at 16:56
  • 1
    I am not sure if that helps, I changed the input to JSON as well. – user1744413 May 04 '14 at 16:59

1 Answers1

1

you could try something like this:

var dataList = {};
function addData(name){
    if( dataList[name] == undefined)
        dataList[name] = [];
    for (var i = 1; i < arguments.length; i++) {
        dataList[name].push(arguments[i]);
    }

}
function packData(){
    var pack = []
    for(var e in dataList){
        pack.push({
            name: e, 
            data:dataList[e].sort(function(a,b){return a-b})
        });
    }
    return pack;
}

addData("Joe", 1);
addData("Jan", 2, 10);
addData("Joe", 3, 5, 10, 18, 500);
addData("Jan", 4);
addData("Joe", 5);
addData("Jan", 6);
console.log( packData() );

use addData(name, data); to append data to a name and afterwards pack this data with packData()

EDIT: Sry switched to PHP for a while... fixed the script XD

Bellian
  • 2,009
  • 14
  • 20