[Solved] JavaScript, find lowest value in array of objects


You have 2 options:

  1. the faster one- a simple loop where you always keep the lowest one you found so far:

    let lowest = arr[0];
    for(let i=1; i<0; i++)
    {
        if(arr[i] < lowest) // I used < for the example, enter your comparison logic here
         lowest = arr[i];
    }
    
  2. The less efficient way, but might help you later in your code: sorting the array using ‘Array.sort’ method:

    array.sort( (el1, el2) => {
      if(el1 > el2) // again- I used > sign, put the actual comparison logic here, i.g: el1.coordinateid[0] > el2.coordinateid[0] and so on
        return -1;
      else return 1;
    });
    

Note that the sort array work like that: you give it a callback function which gets 2 elements of the array. If you return -1 it means the first element that passed should come before the 2nd element when they are put back in the sorted array, 0 means they’re equal and 1 means that the 2nd element passed to the function should come first.
See the sort method link above for more explanation.

0

solved JavaScript, find lowest value in array of objects