I have the following json:
And I need to split it into a number of arrays depending on the width of the browser.
Here is my javascript:
var imageArray = jQuery.parseJSON(data);
var splitArrayInto = jQuery(document).width() / 300;
splitArrayInto = Math.floor(splitArrayInto);
var seperateImageArrays = array_fill(0, splitArrayInto);
for(var i = 0; i < splitArrayInto; i++){
seperateImageArrays[i % splitArrayInto][i] = imageArray[i];
}
When I run that, I get
can’t convert undefined to object
seperateImageArrays[i % splitArrayInto][i] = imageArray[i];
Any help is appreciated.
EDIT:
Forgot to add the array_fill function.
//PHP array_fill
function array_fill (start_index, num, mixed_val) {
var key, tmp_arr = {};
if (!isNaN(start_index) && !isNaN(num)) {
for (key = 0; key < num; key++) {
tmp_arr[(key + start_index)] = mixed_val;
}
}
return tmp_arr;
}
When you call
array_fillyou’re not passing the third parameter, so all your array elements are set toundefined… andundefinedcan’t be dereferenced with[i].Consider passing
[]as the third parameter instead.