I am still new to using JSON files . is there a way to get use data from my json file in a js file that will be called by a button click in another html file. the function is in the script.js
----------
\
----> assets
| \
| ----> Data
| \
| ---->Data.JSON
\
----> js
| \
| ----> script.js
|
\
---->Files
\
---->page.html
function Replace(Data)
{
const obj = JSON.parse(Data);
var length = Object.keys(obj.employees).length;
console.log(length);
document.getElementById('test').innerHTML = "";
for(let i = 0; i < length; i++)
{
document.getElementById('test').innerHTML += "<br>"+obj.employees[i].firstName + " " + obj.employees[i].lastName ;
console.log(obj.employees[i].firstName + " " + obj.employees[i].lastName);
}
}
Could you do something like this?
/*
Example Data:
{
employees: [
{
firstName: "John",
lastName: "Doe",
}
]
}
*/
const data = require("./assets/Data/Data.json");
function appendData() {
data.employees.forEach((employee) => {
document.getElementById(
"test"
).innerHTML += `<br>${employee.firstName} ${employee.lastName}`;
});
}
appendData()