I am doing a fetch request to receive all my json data and store it in my current state, afterwards I want to call a function that will change the content of one of the nested json objects inside of an array.
I've looked at similar questions like this one but I can't seem to figure it out.
Here is the structure of my received json data:
[
{
"id":"George",
"top":[
{
"performance":4289000,
"day":"Sunday",
"penalties":"none"
},
{
"performance":4259000,
"day":"Saturday",
"penalties":"two"
}
]
},
{
"id":"Maya",
"top":[
{
"performance":4139000,
"day":"Monday",
"penalties":"none"
},
{
"performance":4439000,
"day":"Sunday",
"penalties":"one"
}
]
}
]
how can I perform a function on the performance object of the array top and pass it to the full state? For an example, how can I call a function for transforming milliseconds to a duration on this nested object?
Here is my function for changing time duration:
function parseMillisecondsIntoReadableTime(milliseconds) {
//Get hours from milliseconds
var hours = milliseconds / (1000 * 60 * 60);
var absoluteHours = Math.floor(hours);
var h = absoluteHours > 9 ? absoluteHours : '0' + absoluteHours;
//Get remainder from hours and convert to minutes
var minutes = (hours - absoluteHours) * 60;
var absoluteMinutes = Math.floor(minutes);
var m = absoluteMinutes > 9 ? absoluteMinutes : '0' + absoluteMinutes;
//Get remainder from minutes and convert to seconds
var seconds = (minutes - absoluteMinutes) * 60;
var absoluteSeconds = Math.floor(seconds);
var s = absoluteSeconds > 9 ? absoluteSeconds : '0' + absoluteSeconds;
return h + ':' + m + ':' + s;
}
Question:
How can I apply the function parseMillisecondsIntoReadableTime to the performance object inside the top array and set it to this.state fore I render it to the html page?
performancevalue is only for presentational purposes you could call theparseMillisecondsIntoReadableTimein the render. Like a filter.