Undefined element in json object

I try to print all the values ​​in the JSON list with for each, and each time this mysterious undefined variable appears. 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 range)

 undefined**some**: json **list**: that **says**: undefined 

Where is this thing <undefined "?

+4
source share
2 answers

The problem is that s is initially undefined .

change var s; on var s = '';

+6
source

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

0
source

Source: https://habr.com/ru/post/1501222/


All Articles