I have a json file as below. I try to map the items and show the names and images when I select values from radio buttons. But map function is not working. How can I fix this problem ?
below is the html code
<form id="frameworksForm">
<label for="all">
<input type="radio" name="frameworksSelection" id="all" value="all">All</label>
<label for="backend">
<input type="radio" name="frameworksSelection" id="backend" value="backend">Backend</label>
<label for="frontend">
<input type="radio" name="frameworksSelection" id="frontend" value="frontend">Frontend</label>
<label for="mobile">
<input type="radio" name="frameworksSelection" id="mobile" value="mobile">Mobile</label>
</form>
<div id="frameworksDisplay"></div>
</div>
{
"frameworks": {
"backend": [
{ "name": "nodeJS", "imgURL": "./img/node.png" },
{ "name": "Django", "imgURL": "./img/django.png" },
{ "name": "Ruby on Rails", "imgURL": "./img/rails.png" },
{ "name": "laravel", "imgURL": "./img/laravel.png" }
],
"frontend": [
{ "name": "ReactJS", "imgURL": "./img/react.png" },
{ "name": "Angular", "imgURL": "./img/angular.png" },
{ "name": "vueJS", "imgURL": "./img/vue.png" },
{ "name": "JQuery", "imgURL": "./img/jquery.png" }
],
"mobile": [
{ "name": "flutter", "imgURL": "./img/flutter.png" },
{ "name": "React Native", "imgURL": "./img/react.png" }
]
}
}
document.querySelector('#frameworksForm').addEventListener('click', () => {
fetch('data.json')
.then(response => response.json())
.then(data => console.log(Object.entries(data)));
let results = document.querySelector('input[name = frameworksSelection]:checked').value;
if (results === "frontend") {
return frameworks.frontend.map(item => console.log((item.name)))
}
else if (results === "all") {
return frameworks.all.map(item => (item.name))
}
else if (results === "backend") {
return frameworks.backend.map(item => (item.name))
}
else if (results === "mobile") {
return frameworks.mobile.map(item => (item.name))
}
})
I get below response from data in the console. But I cannot get the names and the images in my <div id="frameworksDisplay"></div>
. I converted the json data from object to an array but still I get an error like Cannot read property 'map' of undefined at HTMLFormElement.