I've looked up and have seen different iterations of this, but nothing is working. In this case, the variables I want are in a for loop within function within a function. I've tried declaring variables first, and then defining them within the function. I've tried calling the functions within themselves to try and retrieve it outside of the functions, among other things.
Desired Outcome : I just want to call the variable "FIRM" outside of the function that I created within the function (so I can make the variable a global one). Or I just want the variable FIRM to be created as a global variable itself.
This is very confusing because when I also try GetData() outside of itself to get a variable, the function is not recognized.
JS:
var FIRM;
function GetData() {
fetch('https://hazards.fema.gov/gis/nfhl/rest/services/FIRMette/NFHLREST_FIRMette/MapServer/1/query?where=&text=&objectIds=&time=&geometry=-96.6666%2C32.333&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&distance=&units=esriSRUnit_Foot&relationParam=&outFields=FIRM_PAN%2C+EFF_DATE&returnGeometry=false&returnTrueCurves=false&maxAllowableOffset=&geometryPrecision=&outSR=&havingClause=&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&gdbVersion=&historicMoment=&returnDistinctValues=false&resultOffset=&resultRecordCount=&returnExtentOnly=false&datumTransformation=¶meterValues=&rangeValues=&quantizationParameters=&featureEncoding=esriDefault&f=pjson')
.then(function (response) {
return response.json();
})
.then (function (data) {
appendData(data);
})
.catch(function (err) {
console.log('error: ' + err);
});
function appendData(data) {
for (let obj of data['features']) {
let FIRM = obj['attributes']['FIRM_PAN'];
let FIRM_USTdate = obj['attributes']['EFF_DATE']
console.log(FIRM);
let FIRMdate = new Date(FIRM_USTdate);
console.log(FIRMdate.toLocaleDateString());
}
}
};
console.log(FIRM);
HTML:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>JSON Test</title>
</head>
<body>
<button id="SearchButton" onclick="GetData()">Click Me</button>
<div id="myData"></div>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.