Check out my code below:
var data = require('./campSample.json');
var dataArray = data.resultset.result;
var fs = require('fs');
for(var i = 0; i < dataArray.length; i++){
fs.writeFile("./campList", dataArray[i]["-facilityName"], function (err) {
if(err){
return console.log(err);
}
});
}
I am attempting to iterate through each item in the array using a for loop, extract the facilityName property, and write it to a file. However, when I execute the code above, only the facilityName of the first item in the array is being written. Is there a more efficient approach to achieve this task? The campSample.json provided here is just a sample, with the actual array containing around 4000 items. Here is a snippet of the contents of campSample.json:
{
"resultset": {
"-count": "4904",
"-resultType": "campgrounds",
"result": [
{
"-availabilityStatus": "N",
"-contractID": "GA",
"-contractType": "STATE",
"-facilityID": "530145",
"-facilityName": "A. H. STEPHENS STATE HISTORIC PARK",
"-faciltyPhoto": "/webphotos/GA/pid530145/0/80x53.jpg",
"-latitude": "33.5633333",
"-longitude": "-82.8966667",
"-shortName": "C145",
"-sitesWithAmps": "Y",
"-sitesWithPetsAllowed": "Y",
"-sitesWithSewerHookup": "N",
"-sitesWithWaterHookup": "Y",
"-state": "GA"
},
{
"-availabilityStatus": "N",
"-contractID": "OH",
"-contractType": "STATE",
"-facilityID": "960023",
"-facilityName": "A.W. MARION STATE PARK",
"-faciltyPhoto": "/webphotos/OH/pid960023/0/80x53.jpg",
"-latitude": "39.6336111",
"-longitude": "-82.8747222",
"-shortName": "P023",
"-sitesWithAmps": "Y",
"-sitesWithPetsAllowed": "Y",
"-sitesWithSewerHookup": "N",
"-sitesWithWaterHookup": "N",
"-state": "OH"
},
{
"-availabilityStatus": "N",
"-contractID": "NRSO",
"-contractType": "FEDERAL",
"-facilityID": "72346",
"-facilityName": "ACKER ROCK LOOKOUT",
"-faciltyPhoto": "/webphotos/NRSO/pid72346/0/80x53.jpg",
"-latitude": "43.0523056",
"-longitude": "-122.6456111",
"-shortName": "ARCL",
"-sitesWithAmps": "N",
"-sitesWithPetsAllowed": "Y",
"-sitesWithSewerHookup": "N",
"-sitesWithWaterHookup": "N",
"-state": "OR"
}
]
}
}