I have a function, which contains a sub function. I need to return a variable from the sub function however I am not sure how (I am new to JS).
I also want to avoid using a global variable, as I have about 15 more functions similar to this.
It shows the error, x is not defined.
return data.rr_vin
^
ReferenceError: data is not defined
I have tried declaring the variable as an attribute of the function, i.e. function.myvar
, however I still get the same issue.
function find_num() {
fetch("somefile.json")
.then(res => res.json())
.then(data => {
a = data.numdata.filter(el => el.Process==="Process1").map(o => o.Finish_Time);
b = recent_time(a)
data.x= Math.max(data.numdata.filter(el => el.Finish_Time===b).map(o => o.Shortnum));
console.log(data.x)
return data.x
})
return data.x
}
Ideally I want to call on the find_num()
function to return x.