Javascript - Maximum

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript)

1 - About

Function - Maximum (Max) in javascript

2 - Snippet

2.1 - Math.max

  • Numerical order comparison

data=[
     {x: 20, y: "20"}
   , {x: 3, y: "3"}
   ]
 
let MAX_X = Math.max(...data.map(d => d.x));
let MAX_Y = Math.max(...data.map(d => d.y));
 
console.log("MAX_X: "+MAX_X)
console.log("MAX_Y: "+MAX_Y)

Advertising

2.1.1 - d3.max

From d3-array

  • d3.max is a the natural order comparison. ie
    • the number 20 is bigger than the number 3
    • but the letter 3 is bigger than the letter 20 (3>2)
  • d3.max ignore NaN or undefined values

Example:

data=[
     {x: 20, y: "20"}
   , {x: 3, y: "3"}
   ]
let MAX_X = d3.max(data, d => d.x);
let MAX_Y = d3.max(data, d => d.y);
console.log("MAX_X: "+MAX_X);
console.log("MAX_Y: "+MAX_Y);
  • Tip, you can use d3-array extent to calculate min and max aggregation in one call.
[MIN_X, MAX_X] = d3.extent(data, d => d.x);
[MIN_Y, MAX_Y] = d3.extent(data, d => d.y);
console.log("MIN_X: "+MIN_X);
console.log("MIN_Y: "+MIN_Y);

3 - Documentation / Reference

web/javascript/max.txt · Last modified: 2019/02/16 17:40 by gerardnico