简体   繁体   中英

Node.js/Javascript Multidimensional Array with dynamic Key

I request some data from my database which returns me an Value which is the Type of an int.

var id = databaseReturn["id"]; //-> int 2

Now when I try to make an Array with and key of that value it doesnt work because it is an int, but even after converting it to a String it doesnt work.

id = String(id); //Should be -> String "2"
array[id] = []; //array[2] = [] but should be array["2"] = []

How do I fix that ?

Don't use arrays if you're not going to use int indexers. Just use Object for it:

var obj = {};
obj[id] = //whatever

When you use objects, it will convert the key automatically to string too.

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