Skip to content

Instantly share code, notes, and snippets.

@rd13
Created April 11, 2013 22:49
Show Gist options
  • Save rd13/5367834 to your computer and use it in GitHub Desktop.
Save rd13/5367834 to your computer and use it in GitHub Desktop.
Javascript function to pre-populate multiple 2D arrays
populateEmptyArrays = function(vars, num) {
//Function to pre-populate multiple 2D arrays
//vars = Array of variable names to pre-populate with arrays
//num = Array size
if(!(vars instanceof Array) || isNaN(num)) return;
for(i in vars) {
window[vars[i]] = (function(a){ while(a.push([]) < num); return a})([])
}
}
populateEmptyArrays(['A','B','C'], 6);
console.log(A); //[[], [], [], [], [], []]
console.log(B); //[[], [], [], [], [], []]
console.log(C); //[[], [], [], [], [], []]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment