Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this Javascript json array object;

var dataJson=[ [{v:1},{v:90}],[{"v":2},{"v":"33.7000"}] ];

I want to append this array object dataJson to another object such that it looks like this;

var chartObject = {"cols": [
    {id: "t", label: "t_label", type: "number"},
    {id: "s", label: "s_label", type: "number"} 
], "rows": [
    {c: 
        [{v:1},{v:90}] //dataJson[0]
    },
    {c: 
        [{"v":2},{"v":"33.7000"}] ////dataJson[1]
    }
]};

How do I use a for loop to insert dataJson elements into chartObject? I am sorry I am quite new to javascript and can't even produce some starting code. Thank you very much for any help.

share|improve this question
    
google about javascript array push. chartObject.push(dataJson) –  Aamir Afridi Apr 4 '14 at 13:34

3 Answers 3

up vote 2 down vote accepted

Try this:

...
  ], "rows": dataJson.map(function(row) {return {c:row};})
};
share|improve this answer
    
My deepest apologies for saying your answer did not work just now. It worked perfectly. Thank you very much for your expert answer and time. It is an elegant and short answer. But I have problems understanding it now as I am a newbie. Will study this short line carefully. Wish I could vote your answers a few times more to apologize for the earlier comment. –  user3293156 Apr 4 '14 at 14:07
    
Would you mind explaining the code? It is short and yet hard to understand. But it works. –  user3293156 Apr 4 '14 at 14:18
    
I understand your code now. Elegant! Brilliant!!! –  user3293156 Apr 4 '14 at 14:22
    
.map just applies the function you give it to each element in the array. In this case, it simply returns an object that "wraps" the row. –  Niet the Dark Absol Apr 4 '14 at 14:22
    
Is this programming style related to functional programming? –  user3293156 Apr 4 '14 at 14:23

Using the simple and clean way:

var chartObject = {"cols": [
    {id: "t", label: "t_label", type: "number"},
    {id: "s", label: "s_label", type: "number"} 
]};

var dataJson=[ [{v:1},{v:90}],[{"v":2},{"v":"33.7000"}] ];

chartObject["rows"] = [];   // start with empty array

// iterate over first dataJson array
for(var i = 0, len = dataJson[0].length; i < len; i++){ 
  // push in array `c` mapped to the i-th element of dataJson[0]
  chartObject["rows"].push({c : dataJson[0][i]["v"]});
}

console.log(chartObject);

DEMO

Ignore those [object Object] in DEMO

Sample Output:

{
    cols: [{
        id: "t",
        label: "t_label",
        type: "number"
    }, {
        id: "s",
        label: "s_label",
        type: "number"
    }],
    rows: [{
        c: 1
    }, {
        c: 90
    }]
}
share|improve this answer

Javascript objects are pretty amazing things. Just define a new field in chartObject as an array, and then push whatever json data you want into it. It looks you want rows to be an array of objects which have an identifier for each json object, but unless you explicitly want to name each dataJson with a string, then just use an indexed array:

chartObject["rows"] = [];

for(var i = 0; i < dataJson.length; i++) {
    chartObject["rows"].push(dataJson[0]);
}

Now you can access each piece of data with:

chartObject["rows"][index]

And each field in the data with:

chartObject["rows"][index]["v"]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.