JavaScript || How To Get The Minimum & Maximum Values In A Simple & Object Array
The following is a module with functions which demonstrates how to get the minimum and maximum values in a simple and object array.
The functions on this page allows for an optional selector function as a parameter, which makes them general enough to work on arrays of any type.
1. Simple Array
The example below demonstrates the use of ‘Utils.arrayMin‘ and ‘Utils.arrayMax‘ to get the minimum and maximum values from a simple array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// Simple Array <script> (() => { // Simple array let values = [ 4294967296, 1991, 466855135, 1987, 81125, ]; // Get the minimum and maximum array value console.log( Utils.arrayMin(values) ); console.log( Utils.arrayMax(values) ); })(); </script> // expected output: /* 1987 4294967296 */ |
2. Object Array
The example below demonstrates the use of ‘Utils.arrayMin‘ and ‘Utils.arrayMax‘ to get the minimum and maximum values from an object array.
In this example, a selector is used, which extracts the value to be used in the evaluation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// Object Array <script> (() => { // Object array let users = [ {USER:"bob", SCORE:2000, TIME:32, AGE:16, COUNTRY:"US"}, {USER:"jane", SCORE:4000, TIME:35, AGE:16, COUNTRY:"DE"}, {USER:"tim", SCORE:1000, TIME:30, AGE:17, COUNTRY:"UK"}, {USER:"mary", SCORE:1500, TIME:31, AGE:19, COUNTRY:"PL"}, {USER:"joe", SCORE:2500, TIME:33, AGE:18, COUNTRY:"US"}, {USER:"sally", SCORE:2000, TIME:30, AGE:16, COUNTRY:"CA"}, {USER:"yuri", SCORE:3000, TIME:34, AGE:19, COUNTRY:"RU"}, {USER:"anita", SCORE:2500, TIME:32, AGE:17, COUNTRY:"LV"}, {USER:"mark", SCORE:2000, TIME:30, AGE:18, COUNTRY:"DE"}, {USER:"amy", SCORE:1500, TIME:29, AGE:19, COUNTRY:"UK"} ]; // Get the minimum and maximum user score console.log( Utils.arrayMin(users, (user) => user.SCORE) ); console.log( Utils.arrayMax(users, (user) => user.SCORE) ); })(); </script> // expected output: /* 1000 4000 */ |
3. Mixed Array
The example below demonstrates the use of ‘Utils.arrayMin‘ and ‘Utils.arrayMax‘ to get the minimum and maximum values from a mixed array.
In this example, a selector is used, which extracts the value to be used in the evaluation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// Mixed Array <script> (() => { // Mixed array let mixedArray = [ [0, 'Aluminium', 0, 'Francis'], [1, 'Argon', 1, 'Ada'], [2, 'Brom', 2, 'John'], [3, 'Cadmium', 9, 'Marie'], [4, 'Fluor', 12, 'Marie'], [5, 'Gold', 1, 'Ada'], [6, 'Kupfer', 4, 'Ines'], [7, 'Krypton', 4, 'Joe'], [8, 'Sauerstoff', 0, 'Marie'], [9, 'Zink', 5, 'Max'] ]; // Get the minimum and maximum name length at index 1 console.log( Utils.arrayMin(mixedArray, (item) => item[1].length) ); console.log( Utils.arrayMax(mixedArray, (item) => item[1].length) ); })(); </script> // expected output: /* 4 10 */ |
4. Utils Namespace
The following is the Utils.js Namespace. Include this in your project to start using!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
// ============================================================================ // Author: Kenneth Perkins // Date: Sept 16, 2020 // Taken From: http://programmingnotes.org/ // File: Utils.js // Description: Javascript that handles general utility functions // ============================================================================ /** * NAMESPACE: Utils * USE: Handles general utility functions. */ var Utils = Utils || {}; (function(namespace) { 'use strict'; // Property to hold public variables and functions let exposed = namespace; /** * FUNCTION: arrayMax * USE: Returns the maximum value in a sequence. * @param array: Array to determine the maximum value of. * @param selector: Optional. A transform function to extract values from each element. * @return: The maximum value in the sequence. */ exposed.arrayMax = (array, selector = null) => { let value = arrayMinMax(array, compareType.max, selector); return value; } /** * FUNCTION: arrayMin * USE: Returns the minimum value in a sequence. * @param array: Array to determine the minimum value of. * @param selector: Optional. A transform function to extract values from each element. * @return: The minimum value in the sequence. */ exposed.arrayMin = (array, selector = null) => { let value = arrayMinMax(array, compareType.min, selector); return value; } let arrayMinMax = (array, type, selector = null) => { if (typeof selector !== 'function') { selector = (item) => item; } let value = array .map((item) => selector.call(array, item)) .reduce((x, y) => compare(x, y, type) ? x : y); return value; } let compare = (valueX, valueY, type) => { let result = valueX > valueY ? 1 : -1; if (type === compareType.min) { result *= -1; } return result > 0; } let compareType = Object.freeze({min: 1, max: 2}); (function (factory) { if (typeof define === 'function' && define.amd) { define([], factory); } else if (typeof exports === 'object') { module.exports = factory(); } }(function() { return namespace; })); }(Utils)); // http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply