简体   繁体   中英

What is the javascript equivalent of the PHP auto-assigning array feature?

I am trying to automatically update a javascript array, without specifying a number or string for the key. The value should just take up the next numeric key in the array.

In php you can do this:

<? 
myarray = array();
myarray[] = '1';
myarray[] = '2';
myarray[] = '3';

//this is equivalent to myarray[1] = '1', myarray[2] = '2', myarray[3] = '3'; 


?>

how can I do this in javascript?

this throws an error

$(function(){

var optionset = [];        

optionset[] = 'a';
optionset[] = 'b';

}); 
optionset.push('a', 'b');

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push

Mutates an array by appending the given elements and returning the new length of the array.

 optionset.push('a');
 optionset.push('b');

See push() on W3Schools for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM