user3809938
user3809938

Reputation: 1304

Uncaught SyntaxError: Unexpected identifier in JavaScript when creating a map

I am using jsp and creating a JavaScript map the following way:

map = new Object();
<c:forEach items="${companyNames}" var="companyName">
  map[${companyName[1]}] = ${companyName[0]};
</c:forEach> 

However I keep on getting the following error

Uncaught SyntaxError: Unexpected identifier

map = new Object();
map[Citigroup] = 1;
map[HSBC] = 2;
map[Credit Suisse] = 3;

When I go on the Chrome console and see the error source I see a red line under Suisse, what is the issue?

Upvotes: 0

Views: 954

Answers (1)

Gratus D.
Gratus D.

Reputation: 877

You probably need to wrap your keys in quotation marks.

Something like:

    map = new Object();
   <c:forEach items="${companyNames}" var="companyName">
      map["${companyName[1]}"] = ${companyName[0]};
  </c:forEach> 

My syntax above may not be 100% correct but essentially you want:

map["Credit Suisse"] = 3;

Upvotes: 1

Related Questions