0

I have variable data having json data as below:

{  
   "label":["ch1","ch2","ch3"],  
   "data": [  
             [-0.001000, -0.562500, 0.001875, 0.002188],  
             [-0.000998, -0.687500, 0.000937, 0.001875]  
           ]   
}  

What I want to split above json data using jquery and get them stored in three variables data1, data2, data3 having json data as below:

{  
   "label":["ch1"],  
   "data": [  
             [-0.001000, -0.562500],  
             [-0.000998, -0.687500]  
           ]  
}  

and

{  
    "label":["ch2"],  
    "data": [  
                [-0.001000, 0.001875],  
                [-0.000998, 0.000937]  
            ]  
}  

and

{  
   "label":["ch3"],  
   "data": [  
             [-0.001000, 0.002188],  
             [-0.000998, 0.001875]  
           ]  
}  

Kindly tell me how to split common json data into three different json data as above using jquery?

1
  • Why insist on using jQuery? It is not some be-all, do everything, don't have to write any javascript yourself API! Commented Jun 6, 2012 at 7:28

1 Answer 1

3

The requirements are vague, but this is what I think you mean:

var main = {  
   "label":["ch1","ch2","ch3"],  
   "data": [  
             [-0.001000, -0.562500, 0.001875, 0.002188],  
             [-0.000998, -0.687500, 0.000937, 0.001875]  
           ]   
}

for (var i in main.label) {
  var k = +i + 1, // make k a 1-based value
  data = [];

  for (var j = 0; i < 2; ++j) {
      data.push([main.data[j][0], main.data[j][k]);
  }

  window['data' + k] = {
    "label": [ main.label[i] ],
    "data": data
  }
}

console.log(data1, data2, data3);
Sign up to request clarification or add additional context in comments.

1 Comment

fine!! but what if i want something like [main.data[j][0], main.data[j][k]] using another loop for j variable dynamically

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.