Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Array reduce used for sum. How to implement exclusions found in a different array?

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..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading