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

224
Views
How to get a specific value out of a JavaScript function?

I have a simple code which returns the highest number value from an array.

const getMax = (data) => Object.entries(data).reduce((max, item) => max[1] > item[1] ? max : item);

var myData1 = { a: 1, b:2, c: 3};
var myData2 = { a: 4, b:2, c: 3};

console.log(getMax(myData1));
console.log(getMax(myData2));

Return result:

[ 'c', 3 ]
[ 'a', 4 ]

This whole function is important to run for other purposes, but please how would I specifically print the output of only first calculated value (so 'c' or 'a') and then print specifically output of the second value (so 3 or 4)?

Thank you.

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

0

A function can only return one value, whether simple or complex. You can store the complex value that a function returns, then use its individual simpler pieces:

const getMax = (data) => Object.entries(data).reduce((max, item) => max[1] > item[1] ? max : item);

var myData1 = { a: 1, b:2, c: 3};
var myData2 = { a: 4, b:2, c: 3};
var result1 = getMax(myData1);
var result2 = getMax(myData1);
var simple1_0 = result1[0];
var simple1_1 = result1[1];
var simple2_0 = result2[0];
var simple2_1 = result2[1];
console.log(simple1_0);
console.log(simple1_1);
console.log(simple2_0);
console.log(simple2_1);

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!