I created a simple Component where I organize names, however I have troubles getting the names in my JSON file.
Here is what my JSON file looks like
{
"tasks" : [
{
"id": 1,
"name": "Studies",
"status": "Completed",
"progress": 100
},
{
"id": 2,
"name": "Writing",
"status": "In Progress",
"progress": 91
}
]
}
And the function in my React Component
function orderA() {
data.tasks.map((t) =>
console.log(t.name)
);
const noms = data.tasks.name;
noms.sort();
console.log(noms);
}
I call the function by clicking on a button, console.log(t.name) show the names I need to use but data.tasks.name gives me an undefined error but if I put names in my const my function is working.
data.tasks.map(t => console.log(t.name)), you should rather usedata.tasks.forEach(t => console.log(t.name)). The result is the same, but the.map-function is meant for tranforming data in an array, while the.forEach-function is meant for doing an operation (or several) on every item in an array (like you're doing). developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…