Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Reduce to return a specific value while supplying an initial value

Hi I have the following object I am reducing to the key that its value is the greatest:

project.approved_account_ids: {
    "sp02.testnet": 3,
    "sp03.testnet": 1
}

Like so..

<Typography variant="body2">
  Architect: {Object.keys(project.approved_account_ids).reduce((a, b) => project.approved_account_ids[a] > project.approved_account_ids[b] ? a : b)}
</Typography>}

But if the object is like

project.approved_account_ids: {}

I get the error - TypeError: Reduce of empty array with no initial value. Which I know it is because there is not always keys and values to reduce.

I tried adding an initial value at the end like

{Object.keys(project.approved_account_ids).reduce((a, b) => project.approved_account_ids[a] > project.approved_account_ids[b], 0 ? a : b)}

But that doesn't work. What would be the best method to check if there are values in the first place before applying reduce()? Any help would be appreciated!!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Assuming that you just want the highest value, so just the actual number?

  • Used Object.entries() on the object found here: obj.project.approved_account_ids
Object.entries(obj).reduce(
// Obj is now an array of pairs: [[key, val], [key, val],...]
  • Next set the initial value to a phony pair: ["key",-1] and then compare that initially which of course be replaced on the first iteration:
(max, [key, val]) =>
    max[1] > val ? max : [key, val], ["key", -1]
// Note the second param is destructured to a pair [key, val]
  • Return the second value of the highest pair: output[1]

In regards to an empty object or no number values it will not fail it will return -1 instead, see objB in example. Also, Object.values() works as well, but decided on Object.entries() just in case the actual key matters as well.

const obj = {
  project: {
    approved_account_ids: {
      "sp02.testnet": 3,
      "sp03.testnet": 1,
      "sp00.testnet": null,
      "sp04.testnet": 5,
      "sp05.testnet": 3
    },
    pending_account_ids: {}
  }
};

const objA = obj.project.approved_account_ids;

const objB = obj.project.pending_account_ids;


function maxV(obj) {
  let output = Object.entries(obj).reduce(
    (max, [key, val]) =>
    max[1] > val ? max : [key, val], ["key", -1]
  )
  return output;
}

console.log('Returning the highest number: maxV(objA)[1]');
console.log(maxV(objA)[1]);

console.log('Returning from an empty object: maxV(objB)[1]');
console.log(maxV(objB)[1]);

console.log('Returning the key of the highest number: maxV(objA)[0]');
console.log(maxV(objA)[0]);

console.log('Returning the key and the highest number as an array: maxV(objA)');
console.log(maxV(objA));

console.log('Returning the key and the highest number as an object: Object.fromEntries([[...maxV(objA)]])');
console.log(Object.fromEntries([[...maxV(objA)]]));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!