I have an Array that looks like this.
ETH: [
[ 1571967200, 'DEPOSIT', 'ETH', 0.68364 ],
[ 1571967189, 'WITHDRAWAL', 'ETH', 0.493839 ],
[ 1571967110, 'DEPOSIT', 'ETH', 0.347595 ],
[ 1571966982, 'WITHDRAWAL', 'ETH', 0.266166 ],
[ 1571966641, 'DEPOSIT', 'ETH', 0.899781 ],
[ 1571966421, 'DEPOSIT', 'ETH', 0.218207 ],
[ 1571966410, 'DEPOSIT', 'ETH', 0.205472 ],
[ 1571966250, 'WITHDRAWAL', 'ETH', 0.761543 ],
[ 1571966124, 'DEPOSIT', 'ETH', 0.66315 ],
[ 1571965892, 'DEPOSIT', 'ETH', 0.554933 ]
],
XRP: [
[ 1571967150, 'DEPOSIT', 'XRP', 0.693272 ],
[ 1571967067, 'WITHDRAWAL', 'XRP', 0.393786 ],
[ 1571966896, 'WITHDRAWAL', 'XRP', 0.81984 ],
[ 1571966868, 'WITHDRAWAL', 'XRP', 0.969999 ],
[ 1571966849, 'WITHDRAWAL', 'XRP', 0.650535 ],
[ 1571966763, 'DEPOSIT', 'XRP', 0.588537 ],
[ 1571966749, 'DEPOSIT', 'XRP', 0.109955 ],
[ 1571966399, 'DEPOSIT', 'XRP', 0.537373 ],
[ 1571966054, 'DEPOSIT', 'XRP', 0.709244 ],
[ 1571965945, 'DEPOSIT', 'XRP', 0.157443 ]
],
This code below loops through the array and adds all deposits and subtracts the withdrawals then groups the result using the Token name.
for(groupName in groupedResult) {
const accumulated = groupedResult[groupName].reduce((acc, curr)=>{
acc[curr[1]] += curr[3];
return acc; // ← return the accumulator to use for the next round
}, {DEPOSIT: 0, WITHDRAWAL: 0})
// console.log(groupName, accumulated);
groupedBalance[groupName] = {
Balance: accumulated['DEPOSIT'] - accumulated['WITHDRAWAL']
};
}
Here is the result
{
ETH: { Balance: 2.05123 },
XRP: { Balance: -0.03833600000000059 },
BTC: { Balance: 3.688666 }
}
The code below loops through the grouped balance, makes an API call to get the exchange rates using the Token name. That's where the awkwardness begins.
for (Gname in groupedBalance){
console.log(Gname);
axios.get('https://min-api.cryptocompare.com/data/price?fsym=' + Gname + '&tsyms=USD&api_key=a437b4c681a4107')
.then(response => {
console.log(Gname);
groupedApiResult[Gname] = {
LastestBalance: ((groupedBalance[Gname].Balance * response.data.USD))
};
})
.catch(error => {
// console.log(error);
});
}
The first console.log(Gname); ouputs the following result.
PS E:\Command Line> get-transactions
ETH
XRP
BTC
The Second console.log(Gname); within the axios request, outputs the following result
PS E:\Command Line> get-transactions
BTC
BTC
BTC
Why am I getting diffrent results yet, they are within the same for Loop?
Here is the Entire Code
let csvData =[];
let res = 0;
let groupedResult =[] ;
const groupedBalance = {};
const groupedApiResult = {};
inputStream.pipe(new CsvReadableStream({ parseNumbers: true, parseBooleans: true, trim: true }))
.on('data', (row)=> {
csvData.push(row);
})
.on('end', function () {
groupedResult = csvData.reduce((acc, curr) => {
if(!acc[curr[2]]){
acc[curr[2]] = [];
}
acc[curr[2]].push(curr);
return acc;
},[]);
for(groupName in groupedResult) {
const accumulated = groupedResult[groupName].reduce((acc, curr)=>{
acc[curr[1]] += curr[3];
return acc; // ← return the accumulator to use for the next round
}, {DEPOSIT: 0, WITHDRAWAL: 0})
groupedBalance[groupName] = {
Balance: accumulated['DEPOSIT'] - accumulated['WITHDRAWAL']
};
}
for (Gname in groupedBalance){
// console.log(Gname);
axios.get('https://min-api.cryptocompare.com/data/price?fsym=' + Gname + '&tsyms=USD&api_key=a437b4c681a49c9b45382a38c319ed0a795b9180041154d9dc5702e534e72107')
.then(response => {
groupedApiResult[Gname] = {
LastestBalance: ((groupedBalance[Gname].Balance * response.data.USD))
};
})
.catch(error => {
// console.log(error);
});
}
});
console.log( groupedBalance);
Another thing, on the last line, when i console.log( groupedBalance); out side the fo loop, I get a blank array but with the for loop, it shows the contents of the array. Why is it behaving like this? Is there i Javascript concept im missing?