-2

I am currently researching the JSON and JS (JSON.parse()). Firstly, I need to write this text through JSON and then to show it with JavaScript in HTML in this format (as a list). I know how to show the first artist but don't know how to show the rest of them.

<html>
<body>
<h2>Artists</h2>
<div id="demo"></div>

<script>

var json =[{
        "name": "Deep purple",
        "title1": "Machine Head",
        "title2": "Stormbringer"
    },
    {
        "name": "Joe Satriani",
        "title1": "Flying in a Blue Dream",
        "title2": "The Extremist",
        "title3": "Shockwave Supernova"
    },
    {
        "name": "Snoop Dogg",
        "title1": "The Doggfather",
        "title2": "Snoopified"
    }
];



var obj = JSON.parse(json);
document.getElementById("demo").innerHTML = '<ul><li>'+ obj[0].name+ '</li>';   //what now???

</script>

</body>
</html>

Any advice would be helpful. enter image description here

2 Answers2

2

var json = [{
    "name": "Deep purple",
    "title1": "Machine Head",
    "title2": "Stormbringer"
  },
  {
    "name": "Joe Satriani",
    "title1": "Flying in a Blue Dream",
    "title2": "The Extremist",
    "title3": "Shockwave Supernova"
  },
  {
    "name": "Snoop Dogg",
    "title1": "The Doggfather",
    "title2": "Snoopified"
  }
];
json.forEach((jsonObject) => {
  var name = jsonObject.name;
  var titles = [];
  Object.keys(jsonObject).filter((key) => {
    return key.startsWith("title")
  }).forEach((title) => {
    titles.push(jsonObject[title]);
  })
  var root = document.getElementById("demo");
  render(root, titles, name)
});

function render(root, array, name) {
  var nameElement = document.createElement('h3');
  nameElement.innerHTML = name;
  root.appendChild(nameElement)
  var ul = document.createElement('ul');
  var array;
  root.appendChild(ul); // append the created ul to the root
  array.forEach(function(item) {
    li = document.createElement('li'); // create a new list item
    li.appendChild(document.createTextNode(item)); // append the text to the li
    ul.appendChild(li); // append the list item to the ul
  });
}
<html>

<body>
  <h2>Artists</h2>
  <div id="demo"></div>
</body>

</html>
samuelj90
  • 6,484
  • 2
  • 35
  • 38
1

Without using document.write.

You can loop through JSON using for, forEach and a more functional way is using Array.map. You can generate your content to append the desired DOM element (div#demo in your case).

var json = [{
    "name": "Deep purple",
    "title1": "Machine Head",
    "title2": "Stormbringer"
  },
  {
    "name": "Joe Satriani",
    "title1": "Flying in a Blue Dream",
    "title2": "The Extremist",
    "title3": "Shockwave Supernova"
  },
  {
    "name": "Snoop Dogg",
    "title1": "The Doggfather",
    "title2": "Snoopified"
  }
];


var content = json.map(function(obj){
  var con = "<h3>" + obj.name + "</h3>";
  con += "<ul>";
  con += Object.keys(obj).map(function(key){
      return key.startsWith("title") ? "<li>" + obj[key] + "</li>" : "";
  }).join('');
  con += "</ul>";
  return con;
}).join('');
document.getElementById("demo").innerHTML = content;
<h2>Artists</h2>
<div id="demo"></div>
Jyothi Babu Araja
  • 8,434
  • 3
  • 28
  • 37