0

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.

3
  • 2
    getMax(myData1)[0] Commented Jun 1, 2022 at 21:30
  • Thanks Roberto. I don't know how to mark your comment as an aswer, but this is what I needed! Commented Jun 1, 2022 at 21:33
  • Why is this even a question? It returns an array, don't you know how to index array elements? Commented Jun 1, 2022 at 21:55

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.