How do I use push() on to a 2-D array?

M

mark4asp

How do I use push on to a 2-D array?

Here is my scenario: I have a 2-D array called aSort. aSort stores the
column number of a html table and the sort order for that column of
the table. (Sort order is 'A' or 'D', A for ascending and D for
descending. Column Number will be: '0', '1', '2', ...). aSort has a
maximum number of rows set by gridMaxSortCols. So the client clicks on
the table header to sort the data and aSort is to remember a stack of
such client clicks (which column was clicked and whether it was
ascending or descending). The size of the stack = gridMaxSortCols,
which is the maximum number of such column sorts to be remembered. In
the case of the same column sorted twice only the most recent sort is
to be remembered.

push is to work like this:
1. If an item is already in the stack it is removed.
2. If the size of the stack == gridMaxSortCols an item is popped.
3. The item is pushed on to the stack.

An item is a pair of values. e.g. if the client clicked the table to
sort the 3rd column (descending) then the item will be '2','D' and I
would call push('2','D').

How do I do this?

aSort.push(); // what do I do here to push '2','D'? where aSort is a 2-
D array.


var gridMaxSortCols = 2
var aSort = new Array(gridMaxSortCols,2);

function push(column,order) {
// If an item is already in the stack it is removed and the new one
added.
// copy all the items which are not this column to a temp array then
set aSort to the values of the temp array.
var aSortTemp = new Array(gridMaxSortCols,2);
for (var i = 0; i < aSort.length ; i++){
if (column != gridMaxSortCols[0]){
aSortTemp[0] = aSort[0];
aSortTemp[1] = aSort[1];
}
aSort = aSortTemp;
//If the size of the stack == gridMaxSortCols an item is popped and
the new one pushed.
if(aSort.length == gridMaxSortCols)
aSort.pop();
//otherwise the item is just pushed.

aSort.push('2','D'); // what do I do here to push '2','D' ?
}
}

PS: which versions of IE with push() and pop() work with?
 
N

Noah Sussman

How do I use push on to a 2-D array?

var myArray = [];
var myItem = [2, "D"];
myArray.push(myItem);
var myOtherItem = [1, "A"];
myArray.push(myOtherItem);

alert(myArray[0][1]); // "D"
alert(myArray[1][1]); // "A"

Note that I'm using
var myArray = [];
to create an array, in place of
var myArray = new Array();
the two statements are equivalent, though the first is imo more
succinct
Here is my scenario: I have a 2-D array called aSort.

I am unable to make sense of the code you posted. It has a typo.
Remove the typo, it has an infinite loop. etc. You'll get much
better answers if you post code that runs.
var gridMaxSortCols = 2

This is a number, but you are use a string below as the first
parameter to your push function, "2" != 2
var aSort = new Array(gridMaxSortCols,2);

Creates a 1-dimensional array: [2, 2] ... I think what you wanted was
var aSort = [ [gridMaxSortCols, 2] ], which creates a 2-dimensional
array with one member -- though your code as written still won't do
what you want
function push(column,order) {
var aSortTemp = new Array(gridMaxSortCols,2);

creates another array, [2, 2]
for (var i = 0; i < aSort.length ; i++){
if (column != gridMaxSortCols[0]){


gridMaxSortCols isn't an array, I think you meant if (column !=
aSort[0]){

assuming that were fixed, the statement will always evaluate true for
the first item in aSort, as 'column' is a string but aSort[0][0] is a
number
aSortTemp[0] = aSort[0];
aSortTemp[1] = aSort[1];
}
aSort = aSortTemp;
if(aSort.length == gridMaxSortCols)
aSort.pop();

aSort.push('2','D'); // what do I do here to push '2','D' ?

Assuming that you meant to call this /outside/ the function
definition, that would result in a 4-member array of two numbers and
two strings: [2, 2, '2', 'D'].

If you want to pass an array as the argument to your function, use
push(['2', 'D']), which passes a single parameter that is an array
with 2 members -- though your code as written still won't do what you
want.

/This/ is where your (infinite) for loop ends.
}

PS: which versions of IE with push() and pop() work with?

I believe 5.5 and up. This page contains an implementation of
Array.push() for IE 5.0.

http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
 
R

RobG

This is a number, but you are use a string below as the first
parameter to your push function, "2" != 2

In which browser? In an ECMAScript Language compliant browser:

2 == '2' // true
2 === '2' // false


[...]
for (var i = 0; i < aSort.length ; i++){
if (column != gridMaxSortCols[0]){


gridMaxSortCols isn't an array, I think you meant if (column !=
aSort[0]){

assuming that were fixed, the statement will always evaluate true for
the first item in aSort, as 'column' is a string but aSort[0][0] is a
number


Only if evaluated with ===.


aSortTemp[0] = aSort[0];
aSortTemp[1] = aSort[1];
}
aSort = aSortTemp;
if(aSort.length == gridMaxSortCols)
aSort.pop();

aSort.push('2','D'); // what do I do here to push '2','D' ?

Assuming that you meant to call this /outside/ the function
definition, that would result in a 4-member array of two numbers and
two strings: [2, 2, '2', 'D'].


aSort has been declared as a global, so it is accessible from inside
the function.
 
N

Noah Sussman

In which browser? In an ECMAScript Language compliant browser:

2 == '2' // true
2 === '2' // false

Um, right. Thanks for catching my error. Hereafter I will refrain
from commenting on code until I've had coffee.
Assuming that you meant to call this /outside/ the function
definition, that would result in a 4-member array of two numbers and
two strings: [2, 2, '2', 'D'].

aSort has been declared as a global, so it is accessible from inside
the function.

Yup. I meant "outside the loop."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top