0

I want to store JSON Object into a variable and format it into this ex. (JAN_2018, FEB_2018, etc.) and the other is ex. (1081136328, 1069248328, etc.)

SCREENSHOT HERE

MY CODE:

var json = $.parseJSON(data);
            $(json).each(function(i,val){
                $.each(val,function(k,v){
                    console.log(k);
                    console.log(v);
                });
            });

I want to put the data (k and v) to a variable and format it to variable 1 =(JAN_2018, FEB_2018, etc.) and variable 2 = (1081136328, 1069248328, etc.)

4
  • How does you JSON data is formatted when you get it? looking the image it appears that is getting each value separated Commented Apr 17, 2018 at 12:12
  • when I put k into a variable and output it outside $(json).each function i can get only one data how can i get all of the data and format it into these (JAN_2018, FEB_2018, etc.) ? Commented Apr 17, 2018 at 12:15
  • you and create array an push into it right ? Commented Apr 17, 2018 at 12:18
  • can you try suggestion given ? Commented Apr 17, 2018 at 12:24

1 Answer 1

1

you can make use of array and push value into it and join array

var k_val = [];
var v_val = [];
var json = $.parseJSON(data);
            $(json).each(function(i,val){
                $.each(val,function(k,v){
                    k_val.push(k)
                    v_val.push(v)
                    console.log(k);
                    console.log(v);
                });
            });
   var k_values= k_val.join();
   var v_values= v_val.join();
   console.log(k_values);
   console.log(v_values);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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