Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   How do I use push() on to a 2-D array? (http://www.velocityreviews.com/forums/t933078-how-do-i-use-push-on-to-a-2-d-array.html)

mark4asp 10-03-2007 11:09 AM

How do I use push() on to a 2-D array?
 
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[i][0]){
aSortTemp[i][0] = aSort[i][0];
aSortTemp[i][1] = aSort[i][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?


Noah Sussman 10-03-2007 02:27 PM

Re: How do I use push() on to a 2-D array?
 
On Oct 3, 7:09 am, mark4asp <mark4...@gmail.com> wrote:
> 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[i][0]){


gridMaxSortCols isn't an array, I think you meant if (column !=
aSort[i][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[i][0] = aSort[i][0];
> aSortTemp[i][1] = aSort[i][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/0...tsbyclassname/


RobG 10-03-2007 09:14 PM

Re: How do I use push() on to a 2-D array?
 
On Oct 4, 12:27 am, Noah Sussman <blink...@gmail.com> wrote:
> On Oct 3, 7:09 am, mark4asp <mark4...@gmail.com> wrote:

[...]
> > var gridMaxSortCols = 2

>
> 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[i][0]){

>
> gridMaxSortCols isn't an array, I think you meant if (column !=
> aSort[i][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[i][0] = aSort[i][0];
> > aSortTemp[i][1] = aSort[i][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.


--
Rob


Noah Sussman 10-04-2007 02:23 AM

Re: How do I use push() on to a 2-D array?
 
On Oct 3, 5:14 pm, RobG <rg...@iinet.net.au> wrote:
> On Oct 4, 12:27 am, Noah Sussman <blink...@gmail.com> wrote:
>
> > On Oct 3, 7:09 am, mark4asp <mark4...@gmail.com> wrote:

> [...]
> > > var gridMaxSortCols = 2

>
> > 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


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."



All times are GMT. The time now is 09:19 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.