admin管理员组文章数量:1332360
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
2 Answers
Reset to default 10optionset.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.
本文标签: What is the javascript equivalent of the PHP autoassigning array featureStack Overflow
版权声明:本文标题:What is the javascript equivalent of the PHP auto-assigning array feature? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742317742a2452157.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论