SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    pushing values in array ??

    Hello forums !!
    I would like to push the values in array(without specifying index).
    Can anybody give me the suggestion.
    Note: It should be equivalent to PHP's
    PHP Code:
    $exp_array = array();
    $exp_array[] = "value1";
    $exp_array[] = "value2"
    (without specifying indexes)
    ...........etc

    Thanks in advance to all of you.

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In Javascript you use the push method of the Array object:

    Code javascript:
    var exp_array = new Array (); 
    exp_array.push('value1');
    exp_array.push('value2');

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var exp_array = [];
    exp_array.push('value1');
    exp_array.push('value2');
    Not supported by some older browsers, notably IE5. There are lots of replacements for it if you look for them on google.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHPycho View Post
    Hello forums !!
    I would like to push the values in array(without specifying index).
    Can anybody give me the suggestion.
    Note: It should be equivalent to PHP's
    PHP Code:
    $exp_array = array();
    $exp_array[] = "value1";
    $exp_array[] = "value2"
    (without specifying indexes)
    ...........etc

    Thanks in advance to all of you.
    I'm not sure why you don't want to specify indices. If you mean that you don't want to have to do:

    Code:
    var i = 0;
    var a = new Array();
    a[i] = 'foo';
    ++i;
    a[i] = 'bar';
    ++i
    etc. then this is an idiom I've seen used often:

    Code:
    var a = new Array();
    a[a.length] = 'foo';
    a[a.length] = 'bar';
    Does that help?

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd use push() too. By the way, it can take multiple arguments, so you don't have to make several calls to it on the same array in a row if you don't want to.
    Quote Originally Posted by Raffles View Post
    Not supported by some older browsers, notably IE5. There are lots of replacements for it if you look for them on google.
    Here's one:
    Code:
    if(![].push){ // for old browsers that don't natively support Array.push()
      Array.prototype.push=function(){
        for(var i=0;i<arguments.length;i++) this[this.length]=arguments[i];
        return this.length;
      }
    }
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


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
  •