You have some errors in your code:
- Use
myArray[i].push( 0 );to add a new column. Your code (myArray[i][j].push(0);) would work in a 3-dimensional array as it tries to add another element to an array at position[i][j]. - You only expand (col-d)-many columns in all rows, even in those, which haven't been initialized yet and thus have no entries so far.
One correct, although kind of verbose version, would be the following:
var r = 3; //start from rows 3
var rows = 8;
var cols = 7;
// expand to have the correct amount or rows
for( var i=r; i<rows; i++ ) {
myArray.push( [] );
}
// expand all rows to have the correct amount of cols
for (var i = 0; i < rows; i++)
{
for (var j = myArray[i].length; j < cols; j++)
{
myArray[i].push(0);
}
}
Answer from Sirko on Stack OverflowYou have some errors in your code:
- Use
myArray[i].push( 0 );to add a new column. Your code (myArray[i][j].push(0);) would work in a 3-dimensional array as it tries to add another element to an array at position[i][j]. - You only expand (col-d)-many columns in all rows, even in those, which haven't been initialized yet and thus have no entries so far.
One correct, although kind of verbose version, would be the following:
var r = 3; //start from rows 3
var rows = 8;
var cols = 7;
// expand to have the correct amount or rows
for( var i=r; i<rows; i++ ) {
myArray.push( [] );
}
// expand all rows to have the correct amount of cols
for (var i = 0; i < rows; i++)
{
for (var j = myArray[i].length; j < cols; j++)
{
myArray[i].push(0);
}
}
In your case you can do that without using push at all:
var myArray = [
[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1]
]
var newRows = 8;
var newCols = 7;
var item;
for (var i = 0; i < newRows; i++) {
item = myArray[i] || (myArray[i] = []);
for (var k = item.length; k < newCols; k++)
item[k] = 0;
}
JavaScript 2-Dimensional Array.push() - Stack Overflow
Creating a two dimensional array using .push method (or best way to do it).
How to push values from 1 dimensional array to 2 dimensional array in Javascript? - Stack Overflow
Create a simple TWO dimensional array in JavaScript. I am not just trying to append value in an array - Stack Overflow
a.push("['','','','','','','','']"); you are pushing a string "...", not an array
This will work fine:
a=[ ['','','','','','','',''] ];
a.push(['','','','','','','','']);
You can use this trick to create a normal array of N elements:
var arr_of_n_elements = Array.apply(null, Array(n));
To create an m x n array, just use this trick twice:
var m_n_arr = Array.apply(null, Array(n)).map(function() {
return Array.apply(null, Array(m));
});
With this approach, each element of this array will be undefined (but it'll be a proper array - iterable, etc. - otherwise). To use a specific value, just add another - filler - map:
var empty_val = '';
var m_n_arr = Array.apply(null, Array(m)).map(function() {
return Array.apply(null, Array(n)).map(function() {
return empty_val;
});
});
If your target platform supports Array.prototype.fill(), it gets even more concise:
var m_n_arr = Array.apply(null, Array(n)).map(function() {
return Array(m).fill(empty_val);
});
Do not fall into the trap of oversimplification: this...
var m_n_arr = Array(n).fill(Array(m).fill(empty_val));
... will create an array of exactly the same objects. Now, when you change the value in one row:
m_n_arr[0][0] = '42';
... it means all the elements of 0-th column become equal to '42' too. And that's hardly what you want to see. )
NAME: ${student.name}
`; `- `
report += `
- Track: ${student.track} `; report += `
- Achievments: ${student.achievements} `; report += `
- Points: ${student.points} `; `
Since I couldn't find anything about this in the documentation or with google, thought I'd make a quick post about it; array_push (and presumably all other similar array functions) can be used with 2d arrays, for example:
array_push(array[0], 42);
If array was previously:
array[0,0] = 0
array[1,0] = 0
it will now be:
array[0,0] = 0
array[0,1] = 42
array [1,0] = 0
Presumably this would work for any multidimensional array (push to array[n][n] to add to 3rd dimension of a 3d array, etc.)
Try thinking about what kind of data structures you can use to make your life easier. For this particular case, you have a list of players that you want to associate some data with. You'd probably use a structure like:
activeRange = [
{
name: 'mitch',
data: []
}
]
When you want to update the data, you'd simply call activeRange[0].data.push(someData).
activeRange is an array of players and each player is represented by an object with some properties, (name, data, etc).
Calling activeRange[0] yields the first player in your array and activeRange[0].data will yield the data associated with that player, which you can then manipulate however you want (push, pop, etc)
Based on your comments, you need a structure more like this
var activeRange = [
{
name: 'mitch',
otherData: [
10,
11,
12,
13
]
},
{
name: 'viper',
otherData: [
//values
]
}
]
you can access that by activeRange[0].otherData[2]
to add to it, just push into the sub array activeRange[0].otherData.push(newValue)
This will add it to the end of arrayA
arrayA.push(arrayZ);
Here's a reference for push: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push
Ok, lots of responses as to how to add items to arrays, but lets start with making your code better:
arrayA = [ //don't use new Array()
[1, 2, 3],
[4, 5, 6]
];
arrayZ = [7,8,9];
There're a few ways you can do this.
You can use the array methods unshift or push
arrayA.unshift(arrayZ) //adds z to the front of arrayA
arrayA.push(arrayZ) //adds z to the end of arrayA
You can also set the location explicitly:
arrayA[0] = arrayZ //overwrites the first element
arrayA[1] = arrayZ //overwrites the second element
arrayA[2] = arrayZ //adds a new element at 2
arrayA[arrayA.length] = arrayZ //essentially the same as using push
You can also splice the new element into the array:
arrayA.splice(1, 0, arrayZ)
1 specifies the start index of the elements being inserted/removed .0 specifies how many elements should be removed, in this case we're adding and not removing any. arrayZ is the element to insert