Ragesh Puthiyedath Raju
Ragesh Puthiyedath Raju

Reputation: 3939

push items in array of array in jQuery

I am a beginner in using jQuery Float Chart. Now I try to bind the chat with server side values. I need to build an array structure like below.

data = [{
    label: 'Test 1',
    data: [
        [1325376000000, 1200],
        [1328054400000, 700], 
        [1330560000000, 1000], 
        [1333238400000, 600], 
        [1335830400000, 350]
    ]
},];

My Server Response

enter image description here

My question is how to push items in this array of array. I already try to build an array like this:

    var data = new Array();
    var chartOptions;

$.each(graphdata, function (key, value) {
    data.push({
        label: value.label,
        data: $.each(value.data, function (key, value) {
            Array(value.X, value.Y);
        })
    })
});

Edits

Graph shows in webpage

enter image description here

enter image description here

But it's not working.

Upvotes: 0

Views: 3036

Answers (1)

user2160375
user2160375

Reputation:

The problems is that $.each return collection that iterates on - the collection you don't want to. You can use underscore library that contains function map to project value into another:

var postData = [{label:"test1", "data": [ {X: "10", Y:"11"}, {X: "12", Y: "13"}] }];

var data = []

$.each(postData, function (key, value) {
    data.push({
        label: value.label,
        data: _(value.data).map(function(innerVal) {
           var arr = new Array();
           arr.push(innerVal.X);
           arr.push(innerVal.Y);
           return arr;
        })
      })
    });

Here is jsFiddle: click!

Upvotes: 1

Related Questions