1

I have some JSON I'm passing to a partial view.

{
"0": {
    "storename": "Park plaza",
    "prediction": "Retail Sites",
    "probability": 0.851005
     },
"1": {
    "storename": "Emory Mills",
    "prediction": "Retail Sites",
    "probability": 0.851005744010425
     },
"2": {
    "storename": "Potamac Heights",
    "prediction": "Retail Sites",
    "probability": 0.851005744010425
    }
}

And here's the code for the loop.

<% for(var i=0; i < tabledata.length; i++) { %>
<tr>
    <td><%= tabledata[i].storename %></td>
    <td><%= tabledata[i].prediction %></td>
</tr>
<% } %>

I'm curious as to why this doesn't work, but without a loop, it does.

<tr>
    <td><%- tabledata["0"].storename %></td>
    <td><%- tabledata["0"].prediction %></td>
</tr>
<tr>
    <td><%- tabledata["1"].storename %></td>
    <td><%- tabledata["1"].prediction %></td>
</tr>
<tr>
    <td><%- tabledata["2"].storename %></td>
    <td><%- tabledata["3"].prediction %></td>
</tr>

I realized that even without the loop, I needed the numbers as strings. However, even when I put i.toString() in the loop, it says that there is a typeerror. I also tried putting in quotes "", as well as single quotes '' but that also didn't work.

Also, the errors I see are Syntax error, unexpected token ILLEGAL in my partial view.

Any thoughts?

Jeremy S
  • 11
  • 3

1 Answers1

0

In your loop i is a integer, in the other it's a string.

Amend the loop so that it is a strong and it'll work (wrap it in single quotes)

Matt The Ninja
  • 2,641
  • 4
  • 28
  • 58
  • Right... but this is the part that confuses me. I tried single quotes and it doesn't work either. When I convert i into a string, it says that there is a typeerror – Jeremy S Apr 12 '15 at 10:05
  • Look up JS concatenate string, create it like that. Also console.log the array to check how you are accessing it is correct and that the variable i is what you expect too – Matt The Ninja Apr 12 '15 at 11:25
  • Console.log() is your friend – Matt The Ninja Apr 12 '15 at 11:25