Following your current pattern, it will work like this:
$('#create_indexed_array').click(function() {
var myArray = [[]];
for (var i = 0; i < 2; i++) {
myArray[0][i] = ""; // create cols
}
for (i = 1; i < 5; i++) {
// create copy of first row in each other row
myArray[i] = myArray[0].slice(0);
}
for (i = 0; i < 5; i++) {
myArray[i][0] = i; // set index
}
alert(JSON.stringify(myArray));
});
Working demo: http://jsfiddle.net/jfriend00/vJDPp/
One thing you have to remember is that assigning an array assigns a reference to that array, not a copy so if you want each element of the array to be different, you have to physically make a copy of the first row to put in the subsequent rows. I'd also recommend changing the name of new_array
because that sounds so much like a function name that it makes the code confusing to read to someone who doens't know it.
If you're just trying to initialize a 2D array to all 9's like in your first code example, then this would be much simpler:
$('#create_indexed_array').click(function() {
var myArray = [];
for (var i = 0; i < 5; i++) {
myArray[i] = [];
for (var j = 0; j < 2; j++) {
myArray[i][j] = 9;
}
}
alert(JSON.stringify(myArray));
});
Or a function version:
function create2DArray(lenX, lenY, initVal) {
var myArray = [];
for (var i = 0; i < lenX; i++) {
myArray[i] = [];
for (var j = 0; j < lenY; j++) {
myArray[i][j] = initVal;
}
}
return(myArray);
}