Currently, I have a function named impactData
which is making an ajax call to retrieve data from a CSV file that I created. Upon success, the retrieved data is passed to my generateImpactData
function, which converts the CSV file into an array of objects. Finally, the array called impactArr
is passed to the drawBarGraph()
function.
While this setup works well, I am now looking to streamline my code by creating a new function called appendImpactData
, which will append some of the data to cards on my website unrelated to the bar graph.
When attempting to pass the impactArr
to the appendImpactData
function, I encounter a return value of undefined, even though the drawBarGraph
function has no problem accessing the data. My goal is to ensure that both functions have access to the impactArr
data after the generateImpactData
function.
Below is the relevant code:
function impactData() {
$.ajax({
type: "GET",
url: data,
async: true,
success: function (data) {
appendImpactData(drawBarGraph(generateImpactData(data))) // trying to pass data to all 3
},
error: function (e) {
// handle exception
console.log("Error occurred while reading resource file: ", e);
}
});
}
The first function passes data from the successful ajax call:
function generateImpactData(data) {
let dataList = [];
let csvObjects = $.csv.toObjects(data);
for (var i = 0; i < csvObjects.length; i++) {
sector = csvObjects[i]["Sector"];
impact = csvObjects[i]["Impact"];
date = csvObjects[i]["Date"];
var arrDataListItems = [
sector,
impact,
date,
];
dataList.push(arrDataListItems);
}
const urlStr = window.location.pathname;
const impactArr = []
const recoverArr = []
if (urlStr.includes('/impact/') {
impactArr.push(dataList[0][2], dataList[0][3], dataList[0][4])
} else if (urlStr.includes('/recovery/') {
recoverArr.push(dataList[2][2], dataList[2][3], dataList[2][4])
}
return { impactArr: impactArr
recoverArr: recoverArr
};
}
The second function, where impactArr data is visible:
function drawBarGraph(impactArr) {
console.log(impactArr) // data is returning fine
}
The third function, where impactArr returns undefined:
function appendImpactData(impactArr) {
console.log(impactArr) // returning undefined, I am attempting to access the data here also
}