skip to Main Content

— My question concerns an array, not an object, which the similar question is for —

I have a multidimensional array, where I need to find the array with the largest number from a set position, then return the first item of that array.

I have this working, but feel there must be a better way to acheive this.

Here is my code, which is working in testing:

slidesArray = [["1",500],["2",750],["3",501]];

var EmpArr = []
var x = 0;
var len = slidesArray.length;
for (x; x < len; x++) {
    EmpArr.push(Math.max(slidesArray[x][1]))
}

var largestResult = Math.max(...EmpArr);

var result = slidesArray.filter(function(v,i) {
    return v[1] === largestResult;
}); 

In this example, I need to check slidesArray[0][1], slidesArray[1][1] and slidesArray[2][1] to find which is the largest number. Then I want to return the first element of the array which contains the largest, so here ‘2’.

3

Answers


    1. You wrote a loop that can be replaced with the map function. This is basically what your wrote but in a longer fashion.

    2. You use filter but you want to find 1 result so the function ‘find’ is more suitable.

    const slidesArray = [["1",500],["2",750],["3",501]];
    
    // Map an array with second entries only, then get the maximum value
    let max = Math.max(...slidesArray.map(entry => entry[1]))
    
    // Find entry with max value
    let result = slidesArray.find(entry => entry[1] === max)
    
    // Show result
    console.log(result[0])
    Login or Signup to reply.
  1. You can simply do it, using Array#reduce() method with a one liner code:

    slidesArray.reduce((acc, val) => {
        return (acc[1] === undefined || val[1] > acc[1] ) ? val : acc;
    }, [[]])[0];
    

    This way you can avoid using Math.max() and multiple iterations.

    Demo:

    var slidesArray = [["1",500],["2",750],["3",501]];
    
    var result = slidesArray.reduce((acc, val) => {
            return (acc[1] === undefined || val[1] > acc[1] ) ? val : acc;
        }, [[]])[0];
    
    console.log(result);
    Login or Signup to reply.
  2. You could introduce a helper that returns an item of a collection based on the highest value.

    function maxBy(iterable, fnValue) {
      var maxItem, maxValue;
      for (const item of iterable) {
        const value = fnValue(item);
        if (value <= maxValue) continue;
        maxItem = item;
        maxValue = value;
      }
      return maxItem;
    }
    

    The above function iterates through an iterable. For each item it executes the fnValue function to get a value. It then compares the value against the highest value found so far. If it’s less than or equal to the max value, it will go to next item. Otherwise the max item and value will be replaced with the current item and value.

    Note that comparisons with undefined always evaluate to false. So the first iteration 500 <= undefined will produce false, thus setting the maxItem and maxValue.

    This would allow you to do the following:

    const slides = [["1",500],["2",750],["3",501]];
    const slide = maxBy(slides, slide => slide[1]);
    console.log(slide[0]);
    
    const slides = [["1",500],["2",750],["3",501]];
    const slide = maxBy(slides, slide => slide[1]);
    console.log(slide[0]);
    
    
    function maxBy(iterable, fnValue) {
      var maxItem, maxValue;
      for (const item of iterable) {
        const value = fnValue(item);
        if (value <= maxValue) continue;
        maxItem = item;
        maxValue = value;
      }
      return maxItem;
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search