I have the following object and function to sum the values of the object keys. How can I do the same thing but make sure not to include accounts in a _ignore array?
const _balances = {
"0x000000000": 100,
"0xCONTRACTOR": 200,
"0xALICE": 300,
"0xBOB": 400,
"0xCHARLIE": 500,
};
function circulatingSupply() {
// Total number of tokens that are currently in circulation and are held by various accounts.
return Object.values(_balances).reduce((a, b) => a + b, 0);
}
const CS = circulatingSupply();
console.info("Circulating Supply:", CS);
So what if I had another array whose account names should not be part of array reduce sum?
const _ignore = [
"0x000000000",
"0xCONTRACTOR",
];
I confess array reduce is a bit overwhelming..
>Solution :
const _balances = {
"0x000000000": 100,
"0xCONTRACTOR": 200,
"0xALICE": 300,
"0xBOB": 400,
"0xCHARLIE": 500,
};
const _ignore = [
"0x000000000",
"0xCONTRACTOR",
];
function circulatingSupply() {
// Total number of tokens that are currently in circulation and are held by various accounts.
return Object.entries(_balances).reduce((a, [key, value]) => _ignore.includes(key) ? a : a + value, 0);
}
const CS = circulatingSupply();
console.info("Circulating Supply:", CS);