-2

I went through multiple StackOverflow answers and googled thoroughly before coming in here to help.

I have an array which includes arrays, as shown below.

Please how would I go about finding an array with the highest first value, in this example 30 and print out the whole array which includes the highest value as the result, so in this case the answer is [ 30, '0.26', [ 'oranges', 'with apples' ], [ 'bananas', 'with apples' ] ] ?

I know how to get the highest value, but I can't figure out how to print the whole array including the highest value.

Thank you!!

 let arr = [
     [
        10,
        '0.07',
        [ 'oranges', 'with apples' ],
        [ 'bananas', 'with apples' ]
      ],
      [
        20,
        '0.15',
        [ 'oranges', 'with apples' ],
        [ 'bananas', 'with apples' ]
      ]
      [
        30,
        '0.26',
        [ 'oranges', 'with apples' ],
        [ 'bananas', 'with apples' ]
      ]
    
    ]
masch1na
  • 97
  • 5
  • Does this answer your question? [Finding the max value of an attribute in an array of objects](https://stackoverflow.com/questions/4020796/finding-the-max-value-of-an-attribute-in-an-array-of-objects) – Wyck Sep 15 '22 at 15:07
  • No it doesn't, because it only returns the single item which is the highest value, I need the whole array containing the highest value returned. I already found the answer and posted it in the answer of this question. – masch1na Sep 15 '22 at 15:11

2 Answers2

0

You will need to scan the array and find out the index which contains the highest number

max = -100//You can do something like negative infinity
highestIndex = null;

arr.forEach((arrayToSearch, index) => {

if(arrayToSearch[0] > max) {
highestIndex = index
}


})

Now hopefully you have the Element index which contains the max number you can just print it out...

arr[highestIndex].forEach(elem=>console.log(elem))
smooth_smoothie
  • 1,319
  • 10
  • 27
  • 40
  • It says arrayToSearch is not defined, if I define it, then it says arr.forEach(arrayToSearch, index => { ^ TypeError: undefined is not a function .. the error arrow is pointing at forEach – masch1na Sep 15 '22 at 14:35
  • I did not try and run this code. But it was missing paranthesis. You should learn more about functional programming in JS – smooth_smoothie Sep 15 '22 at 16:07
0

Found the answer:

const highest = arr.reduce((previous, current) => {
  return current[0] > previous[0] ? current : previous;
});

console.log(highest);

Source: https://daily-dev-tips.com/posts/javascript-find-min-max-from-array-of-arrays/

masch1na
  • 97
  • 5
  • This is almost exactly the code from [the proposed duplicate](https://stackoverflow.com/a/34087850/1563833) but instead of `.y` it's `[0]` (and it's trivial to have renamed and reversed the order of current and previous in your ternary expression.) Not different enough, IMO, not to be a duplicate. – Wyck Sep 15 '22 at 15:29
  • You are right. I didn't see the second answer in the proposed duplicate, I only checked the accepted answer. – masch1na Sep 15 '22 at 19:30