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.

10
  • 2
    What exactly are you having problems with? Don't you know how to add values to an array? Commented May 4, 2014 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 Commented May 4, 2014 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. Commented May 4, 2014 at 16:42
  • 2
    A lost cause, I know, but: that's not JSON. Commented May 4, 2014 at 16:43
  • 1
    I am not sure if that helps, I changed the input to JSON as well. Commented May 4, 2014 at 16:59

1 Answer 1

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

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

6 Comments

dataList[name] .. what's that ? You are suggesting to use String as index ?
an JS Object var a = {foo:"bar"} can be accessed via a.foo or a['foo'] which is usefull in manny cases.
Can you kindly suggest .. how can I sort this on keys ?
And if I want to push two values ..then
you can sort the data as follows: dataList[e].sort(function compare(a, b) {return a-b})
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.