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

Discussion in 'Javascript' started by mark4asp, Oct 3, 2007.

  1. mark4asp

    mark4asp Guest

    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?
    mark4asp, Oct 3, 2007
    #1
    1. Advertising

  2. mark4asp

    Noah Sussman Guest

    On Oct 3, 7:09 am, mark4asp <> 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[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/
    Noah Sussman, Oct 3, 2007
    #2
    1. Advertising

  3. mark4asp

    RobG Guest

    On Oct 4, 12:27 am, Noah Sussman <> wrote:
    > On Oct 3, 7:09 am, mark4asp <> 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[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.


    --
    Rob
    RobG, Oct 3, 2007
    #3
  4. mark4asp

    Noah Sussman Guest

    On Oct 3, 5:14 pm, RobG <> wrote:
    > On Oct 4, 12:27 am, Noah Sussman <> wrote:
    >
    > > On Oct 3, 7:09 am, mark4asp <> 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."
    Noah Sussman, Oct 4, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. XyZaa
    Replies:
    0
    Views:
    549
    XyZaa
    Jul 19, 2007
  2. Daniel Berger

    Testing array.push(array)

    Daniel Berger, Nov 9, 2005, in forum: Ruby
    Replies:
    2
    Views:
    118
    Daniel Berger
    Nov 9, 2005
  3. Replies:
    5
    Views:
    126
    YANAGAWA Kazuhisa
    Mar 8, 2006
  4. samppi
    Replies:
    27
    Views:
    437
    David A. Black
    Dec 5, 2007
  5. Amanda ..
    Replies:
    3
    Views:
    140
    Amanda ..
    Aug 25, 2008
Loading...

Share This Page