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

I am trying to automatically update a javascript array, without specifying a number or string for the k

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';

}); 

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';

}); 
Share Improve this question asked Sep 17, 2010 at 8:44 MazatecMazatec 11.6k23 gold badges74 silver badges108 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10
optionset.push('a', 'b');

https://developer.mozilla/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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742317743a4421180.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信