geoff
geoff

Reputation: 2276

undefined item in json object

I'm trying to output all values in a JSON list with foreach, 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

Answers (2)

orange
orange

Reputation: 8090

Your s is undefined. Try var s = '';

Upvotes: 0

bluetoft
bluetoft

Reputation: 5443

The problem is that s is initially undefined.

change var s; to var s = '';

Upvotes: 6

Related Questions