How to get the maximum value between multiple keys from an array

how to get the maximum value between multiple keys from an array?

I've tried this method below for only three (not multiple) keys .

getMaxValuefromkeys(values: any[], key1: string, key2: string, key3: string) {
    var val1 = Math.max.apply(Math, values.map(function (a) { return a[key1] }));
    var val2 = Math.max.apply(Math, values.map(function (a) { return a[key2]; }));
    var val3 = Math.max.apply(Math, values.map(function (a) { return a[key2]; }));
    if (val1 >= val2 || val1 >= val3) {
        return val1;
    } else if (val2 >= val3 || val2 >= val1) {
        return val2;
    }
    return val3;
}

      


But we need to check more conditions and write more codes if we use multiple keys. So I tried these below codes.

Math.max.apply(Math, values.map(function (a) { return a[key1], a[key2], a[key3]; }));
                                         // where I want to return multiple keys 

      


But it doesn't work. Is there any one line of code available to get the maximum value between multiple keys from an array?

+3


source to share


4 answers


Array#map

each object has a maximum value for it, then find the maximum of the array:

var values = [{ a: 4, b: 3 }, { a: 2, b: 8 }, { a: 1, b: 2 }];

var key1 = 'a', key2 = 'b';

var result = Math.max.apply(Math, values.map(function (a) { return Math.max(a[key1], a[key2]); }));

console.log(result);
      

Run codeHide result




If you want something more flexible that can accept multiple keys:

var values = [{ a: 4, b: 3, c: 23 }, { a: 2, b: 28, c: 13 }, { a: 1, b: 2, c: 1 }];

function getMaxOfKeys(values, keys) {
  return Math.max.apply(Math, values.map(function(obj) {
    return Math.max.apply(Math, keys.map(function(key) {
      return obj[key];
    }));
  }));
}

// or the ES6 equivalent

const getMaxOfKeysES6 = (values, keys) => 
  Math.max(...values.map(
    (obj) => 
      Math.max(...keys.map((key) => obj[key]))
    )
  );

console.log(getMaxOfKeys(values, ['a', 'b', 'c']));
console.log(getMaxOfKeysES6(values, ['a', 'b', 'c']));
      

Run codeHide result


+4


source


(plain ES6 - I don't do TypeScript)

Given your object values

and the set keys

you want to compare, for example:

let values = [
   { key1: 2, key2: 3, key3: 0 },
   { key1: 10, key2: 0, key3: 5 }
];

      

then for each key find the maximum value inside each object and return it as an array, and then find the maximum value of that array.

function getMaxValuefromkeys(values, ...keys) {
    return Math.max(...keys.map(key => Math.max(...values.map(o => o[key]))));
}

      



using:

let max = getMaxValuefromkeys(values, 'key1', 'key2', 'key3');

      

or an array of keys is given:

let max = getMaxValuefromkeys(values, ...keys);

      

+1


source


Math.max.apply(Math, values.map(function (a) { return Math.max(a[key1],a[key2]) }));

      

note that

return a,b;

      

Is the comma operator, so it actually just returns b.

0


source


you can also use ternary operation inside map function and return value

Math.max.apply(Math, values.map(function (a) { return (a[key1] >= a[key2]) ? a[key1] : a[key2] ; }));

      

var values = [{ a: 4, b: 3 }, { a: 2, b: 8 }, { a: 1, b: 2 }];

var key1 = 'a', key2 = 'b';

var result = Math.max.apply(Math, values.map(function (a) { return (a[key1] >= a[key2]) ? a[key1] : a[key2] ; }));

console.log(result);
      

Run codeHide result


0


source







All Articles