0

I have to create array in which key (Index) will be strId (typeof strId is "string") when I am doing so I am getting undefined value in list Below is code.

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

Please can anyone figure out why It's behaving in this manner. Any help will be appreciated.

1
  • You should use an object for this. Use var myRoomIdList = {}; Commented Jun 12, 2014 at 13:12

2 Answers 2

3

[] array is dedicated for the numeric index. You should use {} instead:

var myRoomIdList = {};
Sign up to request clarification or add additional context in comments.

Comments

0

Arrays have numerical identifiers, use a map instead.

var map = {};

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.