Forum Moderators: open

Message Too Old, No Replies

pushing values in array?

         

PHPycho

11:23 am on Jul 3, 2007 (gmt 0)

10+ Year Member



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]$exp_array = array();
$exp_array[] = "value1";
$exp_array[] = "value2";[/php]
(without specifying indexes)
...........etc

Thanks in advance to all of you.

cvele

11:33 am on Jul 3, 2007 (gmt 0)

10+ Year Member



exp_array = new Array;
exp_array[exp_array.length] = "value1";
exp_array[exp_array.length] = "value2";

Bernard Marx

6:31 pm on Jul 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That seems a little verbacious.

exp_array = ["value 1", "value 2"];

Format according to taste, but note that IE's JScript will interpret a trailing comma as denoting an extra, undefined member.

[1,2,]
Javascript: 2 members.
JScript: 3 members.

---
PS Arrays do have a push method too:

var newLengthReturned = arr.push(val);

[edited by: Bernard_Marx at 6:33 pm (utc) on July 3, 2007]