簡體   English   中英

在Javascript中設置字符串類型數組鍵值

[英]Set string type array key value in Javascript

我必須在執行此操作時創建其中鍵(索引)為strId (typeof strId is "string")數組,這樣我在列表中獲得了未定義的值,下面是代碼。

var myRoomIdList = [];
var strId = "43457";

myRoomIdList[strId] = strId;

console.log(myRoomIdList);
console.log(myRoomIdList.length);

console.log(myRoomIdList);
Output: Undefined, undefiend, ......43457 times

console.log(myRoomIdList.length);
Output: 43457

請任何人能弄清楚為什么它會以這種方式運行。 任何幫助將不勝感激。

[]數組專用於數字索引。 您應該改用{}

var myRoomIdList = {};

數組具有數字標識符,請改用映射。

var map = {};

map["ID1"] = "xxx";
map["ID2"] = "yyy";
...

暫無
暫無

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

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