D3.js | d3.scan() function
The d3.scan() function is a built-in function in D3.js which scans the array linearly and returns the index of the minimum element according to the specified comparator. The function returns undefined when there are no comparable elements in the array.
Parameters: This function accepts two parameters which are mentioned above and described below:
- array: This mandatory parameter contains an array of elements whose minimum value is to be calculated and the respective index is to be returned.
- comparator: This parameter is an optional parameter which specifies how the minimum element is to be obtained.
Return value: The function returns a single integer value denoting the index of the minimum element in the array based on the specified comparator.
The below programs illustrate the use of d3.scan() function:
Example 1: This program illustrates the use of d3.scan() with a comparator
Minimum element is 17 present at index: 5 Maximum element is 91 present at index: 2
Example 2: This program illustrate the use of d3.scan() without a comparator
Minimum element is 17 present at index: 5