-5

I have something like :

var array = [
{'a':3,'b':4},
{'a':1,'b':8},
{'a':9,'b':7}]

What is an elegant one line to find the min/max of the key 'a' (max=9, min = 1) ? I know it is simple but i couldn't find a one line solution only loops.

  • What exactly is the expected result? – Sebastian Simon Dec 21 '22 at 08:52
  • I just edited, thanks. max here is 9 and min is 1 – gotiredofcoding Dec 21 '22 at 08:52
  • how about `Math.min(...array.map(x => x.a))` – gog Dec 21 '22 at 08:54
  • 1
    So what is the result, precisely? `1` or `9` or `{ a: 9, b: 7 }` or `{ a: 1, b: 8 }` or `{ min: 1, max: 9 }` or `{ min: { a: 1, b: 8 }, max: { a: 9, b: 7 } }` or something else? Countless options, no example, no effort, no surprise it got downvoted. – Sebastian Simon Dec 21 '22 at 08:54
  • thanks @gog i guess its that simple I am just new to js. – gotiredofcoding Dec 21 '22 at 08:54
  • 6
    I wish the new generation of JS coders stopped looking for inefficient one liners and focus on performance and readability.. Wait, did I just made an old man rant? – Kaddath Dec 21 '22 at 08:55
  • 1
    @gotiredofcoding Not my downvote, but people do tent to downvote questions that don't show any effort, as they come across like "do my work for me" questions. – Ivar Dec 21 '22 at 08:55
  • Use the static and instance methods of [`Array`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). Take your pick: `const values = array.map(({ a }) => a); const [ minValue, maxValue ] = [ Math.min(...values), Math.max(...values) ]; const [ minObjects, maxObjects ] = [ array.filter(({ a }) => a === minValue), array.filter(({ a }) => a === maxValue) ];`. – Sebastian Simon Dec 21 '22 at 09:02
  • 1
    Here's a useless solution that only traverses the array once to get both min and max: `const [min, max] = array.reduce(([min, max], { a }) => [a < min ? a : min, a > max ? a : max], [Infinity, -Infinity])`. – Hao Wu Dec 21 '22 at 09:06
  • 1
    > calls us childish > engages in childish behavior in response to valid criticism about the nature of your question wut – Mike Warren Dec 21 '22 at 09:13
  • Consider `const minMaxObjects = (array, property) => { let min = Infinity, max = -Infinity; const map = array.groupToMap((object) => { if(object[property] < min){ min = object[property]; } if(object[property] > max){ max = object[property]; } return object[property]; }); return { min: map.get(min), max: map.get(max) }; }; const { min, max } = minMaxObjects(array, "a");`, using [`groupToMap`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/groupToMap). Single iteration, yields an array of all objects with minimum value, and another array with maximum. – Sebastian Simon Dec 21 '22 at 09:19

3 Answers3

2

Logic

const array = [{ a: 3, b: 4 },{ a: 1, b: 8 },{ a: 9, b: 7 }];
const max = Math.max(...array.map(({ a }) => a));
console.log(max);

Apply the same logic for min as well and use Math.min

const array = [{ a: 3, b: 4 },{ a: 1, b: 8 },{ a: 9, b: 7 }];
const nodes = array.map(({ a }) => a);
const maxMin = { max: Math.max(...nodes), min: Math.min(...nodes)};
console.log(maxMin);
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
1

you can do something like this

var array = [
  { a: 3, b: 4 },
  { a: 1, b: 8 },
  { a: 9, b: 7 },
];

const arrayA = array.map((v) => v.a);

const maxA = Math.max(...arrayA);
const minA = Math.min(...arrayA);

console.log({ minA, maxA });
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
R4ncid
  • 6,944
  • 1
  • 4
  • 18
0

Tried to keep it in 1 line.

var array = [
  { a: 3, b: 4 },
  { a: 1, b: 8 },
  { a: 9, b: 7 },
];
const result = {
  min: Math.min(...array.map((x) => x.a)),
  max: Math.max(...array.map((x) => x.a)),
};
console.log(result);
Nitheesh
  • 19,238
  • 3
  • 22
  • 49