SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multidim. array on-the-fly

    Ok, maybe on-the-fly is not the correct term to refer to my problem, but here I go.

    I'm relatively new to JS, and I'm actually trying to create a copy of the game "Go" using this language (or rather, using jQuery) [ok, the idea is to join it with ajaj, php and mysql in order to make it multiplayer], anyway the problem is the following: i have to create a multidimensional array based on 2 variables i recieve from a loop.

    The first one is GroupName, the second one is StonePosition. For each GroupName there can be X StonePosition.

    If I had to do it in PHP i would do something like:
    $array[GroupName][] = StonePosition;

    in order to let the array create its index.. which I don't know.

    1) How can I do something like that with JS ?

    2) Any comments in general (like better ways to do things, or whatever) ?
    (Beside the fact that is everything on the same page - it's just a test)

    Thanks :)

    Here the test page:
    http ://k4s.ch/gojs
    (sorry for the italian bits, just try clicking random on the board after some group of stone is made click on "clicca")

    PrintScreen:
    http ://img408.imageshack.us/my.php?image=goaj6rj6.png

    Here the snippet:
    Code:
    //test function to save groups into an array
    $("#asd").click(function(){
        $("#tab td").each(function(){
            //if there is a stone
            if (goban[$(this).data('x')][$(this).data('y')] != 0) {
                //save its coordinates
                var idaa = String.fromCharCode($(this).data('x') + 64) + $(this).data('y');
                var gruppo = $(this).data('gruppo');
                //add to array with groupname and stoneposition
                //???????//
                //test
                $('<li></li>').text('Group:'+gruppo+' Position:'+idaa).appendTo('ol');
            }
        });
    });

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    It seems that you're wanting to add the StonePosition value onto the array, so you can use push to easily add them in. You just have to make sure that the GroupName has been previously set to be an array, which is done by assigning [] to it.

    Code javascript:
    array[GroupName] = [];
    ...
    array[GroupName].push(StonePosition);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jun 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking

    Quote Originally Posted by pmw57 View Post
    It seems that you're wanting to add the StonePosition value onto the array, so you can use push to easily add them in. You just have to make sure that the GroupName has been previously set to be an array, which is done by assigning [] to it.

    Code javascript:
    array[GroupName] = [];
    ...
    array[GroupName].push(StonePosition);
    omg, thanks it works

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Karas View Post
    omg, thanks it works
    You're welcome.

    The push() method came in with JavaScript 1.2, which is supported by version 4 browsers of Netscape and IE. If you'll want to support older browsers you can either change the code to use the following:

    Code javascript:
    array[GroupName][array.length] = StonePosition;

    or you can extend the array object and add the push method

    Code javascript:
    if(!Array.prototype.push) {
    	Array.prototype.push = function () {
    		for (var i = 0; i < arguments.length; i += 1) {
    			this[this.length] = arguments[i];
    		}
    		return this.length;
    	};
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •