9951 explained code solutions for 126 technologies


javascript-d3How do I use the d3.max function in JavaScript?


The d3.max function is used to find the maximum value in an array of numbers. It is part of the d3.js library which provides a wide range of functions for data manipulation and visualization.

Example code

var data = [1, 5, 3, 7, 9, 4];
var max = d3.max(data);
console.log(max);

Output example

9

The code above uses the d3.max function to find the maximum value in the array data. The function takes the array as an argument and returns the maximum value, which in this case is 9.

The d3.max function can also take a second argument, which is a function that is used to compare values in the array. This allows for more complex comparisons, such as finding the maximum value of an array of objects.

Example code

var data = [
  {name: 'John', age: 25},
  {name: 'Jane', age: 30},
  {name: 'Bob', age: 20}
];

var max = d3.max(data, function(d) {
  return d.age;
});
console.log(max);

Output example

30

In the code above, the d3.max function is used to find the maximum age in the array data. The function takes the array and a function as arguments. The function is used to compare the values in the array, in this case the ages of the objects. The d3.max function then returns the maximum value, which in this case is 30.

Helpful links

Edit this code on GitHub