1

I have JSON data that I am trying to store in a 2D array in the format:

[2009-01,324, 1075, 940, 441, 1040, 898, 1343]
[2009-02 295, 958, 904, 434, 1038, 793, 1246 ]

Apologies for the notation visual representation above. I have the JSON data and I am using a for loop to grab the key and its value but im not sure how to store this in a 2d array. I am struggling with this conceptually. Im a beginner and just really want some guidance with this. Thank you.

<script>
$.getJSON('data.json', function (data) {

var response = data;

var listOfTimes = new Array();
for(i = 0; i < 2; i++){ //number of rows...
 for(j = 0; j< 8; j++){ //number of columns

 for(key in response){
 ....not sure what to do here 
 }    
 } 
}

});
</script>

The JSON data is in the format below:

"2009-01": {
    "bbcfour": 324,
    "bbcnews24": 1075,
    "bbcone": 940,
    "bbcthree": 441,
    "bbctwo": 1040,
    "cbbc": 898,
    "cbeebies": 1343
  },
  "2009-02": {
    "bbcfour": 295,
    "bbcnews24": 958,
    "bbcone": 904,
    "bbcthree": 434,
    "bbctwo": 1038,
    "cbbc": 793,
    "cbeebies": 1246
  }
1
  • 1
    your example of a 2D array isn't a 2D array. It are just 2 arrays. Also your json example isn't a valid json string! Commented Feb 22, 2015 at 10:02

4 Answers 4

1

try to loop through all the dates & then inner loop on their properties

<script>
    $.getJSON('data.json', function (data) {
        var arr = [];

        for(var date in data){
            var items = [];
            for(var key in date){
                items.push(date[key]);
            }   
            arr.push(items);
        }    

        console.log(arr); // the requested array
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

jsfiddle demo

var mainobj={
  "2009-01": {
    "bbcfour": 324,
    "bbcnews24": 1075,
    "bbcone": 940,
    "bbcthree": 441,
    "bbctwo": 1040,
    "cbbc": 898,
    "cbeebies": 1343
  },
  "2009-02": {
    "bbcfour": 295,
    "bbcnews24": 958,
    "bbcone": 904,
    "bbcthree": 434,
    "bbctwo": 1038,
    "cbbc": 793,
    "cbeebies": 1246
  }
};

var my2darray=[];

for (var date in mainobj) {
   var temparray=[date];
    for(val in mainobj[date]){
        temparray.push(mainobj[date][val]);
    }
   my2darray.push(temparray);
}
console.log(my2darray);

output:

[["2009-01", 324, 1075, 940, 441, 1040, 898, 1343],
["2009-02", 295, 958, 904, 434, 1038, 793, 1246 ]]

1 Comment

thank you so much, this is exactly what I was after. a 2d array to store all the JSON!
0
var response = data;

var listOfTimes = new Array();
for(i = 0; i < 2; i++){ //number of rows...
 listOfTimes[i]=response[i];
 for(j = 0; j< 8; j++){ //number of columns
 listOfTimes[i]=repsonse[i][j];   
 } 
}

1 Comment

thank you, im not entirely sure how the code listOfTimes[i]=response[i]; works? And response[i] returns an undefined object
0

I think you are looking for this, it will create an array of arrays based on your map. Not sure why can't you use the array data model. (Watch out for that $.each, it requires jQuery)

var resp =  {"2009-01": {
    "bbcfour": 324,
    "bbcnews24": 1075,
    "bbcone": 940,
    "bbcthree": 441,
    "bbctwo": 1040,
    "cbbc": 898,
    "cbeebies": 1343
  },
  "2009-02": {
    "bbcfour": 295,
    "bbcnews24": 958,
    "bbcone": 904,
    "bbcthree": 434,
    "bbctwo": 1038,
    "cbbc": 793,
    "cbeebies": 1246
  }};

var arrays = [];
for (mainKey in resp) { 
  var array = []; 
  arrays.push(array);  
  array.push(mainKey); 
  $.each(resp[mainKey], function(key, value) { 
    array.push(value) 
  }); 
};

4 Comments

thank you, i think this works. But i dont think the main key is being pushed onto the array too? How would i get the mainKey from the array and the values that belong to it outside the for loop?
All the data stored in the arrays variable. It contains arrays generated from your map input parameter. First item in those arrays are the mainKey = your date.
the problem of this would be difficult access to each element i wanted e.g. i wanted to get the data for 2009-01": { "bbcfour": 324}. I could access the array[0] but in order to get to bbcfour's value?
Well ... For what you are asking for, you don't need to change the map into array. You could just get the item like resp["2009-01"]["bbcfour"].

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.