So I think this is likely a scope issue, but am a bit stumped as to why I cannot access the response data from a $.getJSON() request using a variable.
When calling $.getJSON('fruits.json') the data comes back as expected, and I can swap 'fruits.json' for cat+'.json' which also seems to work fine.
However, when evaluating the data response, using 'data.cat.length' comes back undefined, even though cat = fruits which is the name of the object in the fruits.json file.
Hard-coding 'data.fruits.length' works as expected, but swapping 'fruits' for the variable 'cat' returns undefined.
Not sure what I'm doing wrong, but the goal is to have the createQuestion(category) function load up a different JSON file dynamically based on the category input.
function createQuestion(category){
var cat = category;
// JSON Data Import
$.getJSON('fruits.json', function(data) {
// Generate Cards
var cards = [];
var display = [];
for (i=0; i<data.fruits.length; i++) {
cards.push(data.fruits[i]);
}
I assume that var cat = 'fruits' is the same as 'fruits', but seems the data object does not agree. Any thoughts on what I'm doing wrong?