So I basically import a JSON file. I get back many arrays and each array has 4 elements in it. I want to parse the 3rd element from each array into it's own variable array.
$("#nextQ").click(function() {
var Quotes = [];
var totalQ //The total number of available quotes to choose from
//Get quotes from JSON file
$.ajax({
url: '../facts.json',
datatype: 'json',
type: 'get',
success: function(data) {
console.log(data[0][2]); //This WORKS
console.log(data.length); //Returns 64
totalQ = data.length;
for (i = 0; i <= totalQ; i++) {
Quotes[i] = data[3][2]; //This WORKS
Quotes[i] = data[i][2]; //This gives ERROR
}
}
});
});
When I use data[i][2] I get this error: Uncaught TypeError: Cannot read property '2' of undefined. However this error doesn't occur if I use data[6][2] or any other number.
i <= totalQ;toi <totalQ;$.each(data, function() {...lengthreally starts at one, as the length would be1if the array had one item, but that item would be atarray[0]. When you loop, you loop toarray.length - 1, but you're looping untiliis the same as the length, so you're trying to look uparray[length], which doesn't existsdata[data.length]will be undefined, you are trying to get the property ofundefined. The property you are trying to access is2which isundefinded.