I have successfully fetched data from an api URL that I develop and the nested data from api is shown below.
I have logged the result in console log and it is successfully fetched and formatted.
I have tried to display the formatted data in the browser, but no lack.
Html button action on click
<button onclick="fetch();">Click</button>
This where result will be displayed after the all process have done:
<div id="data"></div>
Formatted data:
let data = [
{
"id": 6,
"Name": "Krakatoa",
"Location": "USA",
"History": "The 1883 eruption of Krakatoa",
"Active": "dormant"
},
{
"id": 7,
"Name": "Krakatoa",
"Location": "USA",
"History": "The 1883 eruption of Krakatoa",
"Active": "dormant"
},
{
"id": 9,
"Name": "Krakatoa",
"Location": "KENYA",
"History": "The 1883 eruption of Krakatoa",
"Active": "dormant"
},
{
"id": 10,
"Name": "Krakatoa",
"Location": "KENYA",
"History": "The 1883 eruption of Krakatoa",
"Active": "dormant"
},
{
"id": 11,
"Name": "Krakatoa",
"Location": "AUSTRALIA",
"History": "The 1883 eruption of Krakatoa",
"Active": "dormant"
}
]
Process of formatting data:
data.filter( item => {
iterateObject(item);
});
function iterateObject(obj){
for(prop in obj){
if (typeof(obj[prop]) == "object") {
iterateObject(obj[prop]);
}
else {
if (prop == "Name" || prop == "Location" || prop == "History" || prop == "Active")
{
$('#data').html(prop.toUpperCase() + ': ', obj[prop]);
}
}
}
}
Kindly help I am stuck in displaying data in a browser.
NB: It's only displaying: last array name with data i.e Active: