I've been stuck in this quandary for far too long now and need to hunt for outside expertise. I need to write a simple data transform function in JS to convert an external .geojson file to a format that I can use to build listed elements in the view. The output of the function is 'correct', but it will change based on whether it is inside the $.getJSON() or outside of it (see plunker below). When I say "change I mean that they way my console.log() output looks is different based on whether I am inside or outside the $.getJSON() function. What's completely blowing my mind is that the typeof
is the same for both outputs, but the result inside the $.getJson() is iterable, while the other is NOT. The reason why this is important is that I need the final result to be passed to another function to create the list of elements on the page, therefore it has to be an iterable object. Here's a simplified code snippet of the functions that I am using:
$(document).ready(callTransformData);
var refinedData = [];
function callTransformData() {
var transformedData = transformData();
// build list elements from returned data here
$.each(transformedData, function(index, value) {
console.log(index, value); // because the result returned from transformData() is not iterable, this console.log is never read
});
}
function transformData() {
var prepData = {};
$.getJSON('data.geojson', function(allLibraries) {
$.each(allLibraries['features'], function(index, val1) {
$.each(val1, function(key2, val2) {
if (key2 === 'properties') {
if (!prepData[val2.inst_name_long]) {
var inst_properties = {};
inst_properties.properties = {};
prepData[val2.inst_name_long] = inst_properties;
prepData[val2.inst_name_long].properties.group = val2.group_name;
prepData[val2.inst_name_long].properties.shared_size = val2.holdings_retained_category;
prepData[val2.inst_name_long].properties.all_size = val2.all_titles_considered_retention_category;
}
}
});
});
console.log('prepData INSIDE getJSON()');console.log(prepData); // notice that it is iterable
$.each(prepData, function(key, val) {
refinedData.push({
library: key,
properties: val.properties
});
console.log('refinedData INSIDE the getJSON()');console.log(refinedData); // notice that it is iterable
});
});
console.log('prepData OUTSIDE the getJson()');console.log(prepData); // not iterable
$.each(prepData, function(key, val) {
refinedData.push({
library: key,
properties: val.properties
});
});
console.log('refinedData OUTSIDE the getJSON()');console.log(refinedData); // not iterable
return refinedData;
}
and here's a sample of the data.geojson
data:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"group_name": "ALI",
"inst_name_long": "Indiana University Purdue University at Indianapolis",
"holdings_retained_category": "100,000+",
"all_titles_considered_retention_category": "500,000+"
},
"geometry": {
"type": "Point",
"coordinates": [
-86.1762,
39.7742
]
}
},
{
"type": "Feature",
"properties": {
"group_name": "ALI",
"inst_name_long": "Indiana State University",
"holdings_retained_category": "100,000+",
"all_titles_considered_retention_category": "500,000+"
},
"geometry": {
"type": "Point",
"coordinates": [
-87.4106,
39.4705
]
}
},
{
"type": "Feature",
"properties": {
"group_name": "ALI",
"inst_name_long": "DePauw University",
"holdings_retained_category": "<100,000",
"all_titles_considered_retention_category": "250,000+"
},
"geometry": {
"type": "Point",
"coordinates": [
-86.8616,
39.6404
]
}
}
]
}
I've been stuck on the fact that this must be a scope issue, but I can't for the life of me figure out where I'm floundering here. I've tried moving my two key variables prepData
and refinedData
as internal and global variables but to no avail. The thing that is most bizarre to me is the fact that my chrome console log gives me different outputs based on whether I am inside the $.getJSON() function or not. Clearly, I am missing a key thing here that the JS is doing. Please see my plunker for a quick review of the console.log() outputs to see what I'm talking about here: https://plnkr.co/edit/340oshw78kEAOdFwZpH9?p=info Thanks in advance for any and all assistance, I feel like I'm losing my mind over here!