I have 2 getJSON calls and, the second is dependent on the first. Based on the length of the second one, I want to push values from the first call to an array and use outside of the loop.
I know these are async calls but I need to know how to get these values inside an array and be made to use outside the loop.
First JSON:
let first_json = [
{
"company": "one"
},
{
"company": "two"
},
{
"company": "three"
}
]
The second JSON is dependent on the first one.
Here is my getJSON code:
let arr = [];
$.getJSON(first_json, function(data) {
data.forEach(d => {
let second_json = json_string+d.company;
$.getJSON(second_json, function(two_data) {
if (two_data.length > 0) arr.push(d.company);
});
});
});
console.log(arr);
Right now I am getting back an empty array.
Assuming company: two is empty, arr should return ['one', 'three']
.