I have a simple json file which includes mathematical operational information. I want to lis json data into inside ul, so that, object key should be header with header class, and its values should list after the each header.
My script file:
{
"Operations":{
" Arithmetic Operations" : ["+", "-", "*", "/"],
"Comparision Operations" : ["Equals","Greater than","Greater than equal to","Less than","Less than equal to"]
}
}
fetch('./js/data.json')
.then(function (response) {
return response.json();
})
.then(function (data) {
fetchList(data['Operations'])
});
fetchList = data => {
Object.entries(data).forEach(([key, values]) => {
let list;
values.forEach(item => {
list = `<li class="list">${ item }</li>`;
});
const li =
`<li class="head">${ key }</li>
${ list }
`;
console.log(li)
document.querySelector('#operations .lists').innerHTML += li
});
}
Result shows only last values of each one
<li class="head"> Arithmetic Operations</li>
<li class="list">/</li>
<li class="head">Comparision Operations</li>
<li class="list">Less than equal to</li>
list =just overwrites the value each time. I think you meant to use+=rather than=, after first initialisinglistto an empty string.