In Python I could do something like myMap = {key: [value1, value2]} and then access the value2 using myMap[key][1]
Can I do something like this in JavaScript?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well, you can do this:
JavaScript doesn’t have an “associative array” type, one that combines “map” behavior with array behavior like keeping track of the number of properties. Thus the common thing to do is use a plain object. In modern JavaScript now (2017), there’s an explicit
Mapfacility that allows keys to be of any type, not just strings as when using simple objects.JavaScript is a little bit silly about the object literal notation, in that it won’t let you use reserved words for keys unless you quote them:
The quote syntax allows any string to be used as a property name. To access such properties, you’d use the
[ ]operator