Firstly, I believe you've written your LHS and RHS of =
the wrong way around.
side_to_set = side_to_get;
Next, you can't set a property of undefined, so say you have arr = []
, you can't set the property arr[0].bar
because arr[0]
is undefined. You first have to set arr[0]
.
What does this mean for you? Well, you'll have to create new a new Array for each row of your 2D structure;
var calc = function(){
var nums = [], i, j, k; // remember to `var`
for (i = 0; i < 25; i++) { // keeping with your single for loop
j = i % 5; // code clarity saves lives
k = (i - j) / 5;
if (!nums[j]) // if no array at this index
nums[j] = []; // create one
nums[j][k] = document.getElementById(i).value;
}
return nums; // let you access `nums` outside of `calc`
};
This code accesses your elements in order of their id using one for
, but you can simplify the calculations if you don't mind a nested for
/accessing them in a differed order, as described in Oriol's answer