簡體   English   中英

什么是PHP自動分配數組功能的javascript等價物?

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

我試圖自動更新javascript數組,而不指定鍵的數字或字符串。 該值應該只占用數組中的下一個數字鍵。

在PHP中你可以這樣做:

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

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


?>

我怎么能用javascript做到這一點?

這會引發錯誤

$(function(){

var optionset = [];        

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

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

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

通過附加給定元素並返回數組的新長度來變量數組。

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

有關詳細信息,請參閱W3Schools上的push()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM