Reputation: 2276
I'm trying to output all values in a JSON list with for
each, and every single time this mysterious "undefined" variable pops up. Take a look:
HTML:
<span id="1">
</span>
JavaScript:
var lel =
{
1: {
"some": "json",
"list": "that",
"says": "undefined",
},
}
var s;
for(i in lel[1]) {
s+= '<B>' + i + '</B>' + ": " + lel[1][i] + "<br />";
}
document.getElementById('1').innerHTML = s;
Result (in span)
undefined**some**: json
**list**: that
**says**: undefined
Where is that "undefined" thing coming from?
Upvotes: 4
Views: 308
Reputation: 5443
The problem is that s is initially undefined
.
change var s;
to var s = '';
Upvotes: 6