-4

Consider:

var playlist = {"bad romance":"gaga", "we cry":"the script"};

How can I get string "gaga" from my code? What is the name of my array in JavaScript programming?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • That structure is invalid in Javascript if I am not mistaken. You are confusing Dictionaries/Maps with Arrays. Try using '{' instead of '(' and you will have a valid dictionary and can access through playlist['bad romance'] which means playlist[key] – nunofmendes Dec 20 '14 at 13:14
  • The canonical is *[How to do associative array/hashing in JavaScript](https://stackoverflow.com/questions/1208222)*. – Peter Mortensen Jul 19 '20 at 20:58

1 Answers1

2

Try Object instead of Array:

var playlist = {"bad romance":"gaga", "we cry":"the script"};
alert(playlist["bad romance"]); //gaga