Max Array Value Based on Function

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will dive into a JavaScript programming challenge where we will be tasked with creating a function that returns the maximum value of an array, after mapping each element to a value using the provided function. By using Array.prototype.map() and Math.max(), we will be able to efficiently achieve this task and gain a better understanding of these essential JavaScript methods.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/destr_assign("`Destructuring Assignment`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28329{{"`Max Array Value Based on Function`"}} javascript/data_types -.-> lab-28329{{"`Max Array Value Based on Function`"}} javascript/arith_ops -.-> lab-28329{{"`Max Array Value Based on Function`"}} javascript/comp_ops -.-> lab-28329{{"`Max Array Value Based on Function`"}} javascript/array_methods -.-> lab-28329{{"`Max Array Value Based on Function`"}} javascript/higher_funcs -.-> lab-28329{{"`Max Array Value Based on Function`"}} javascript/destr_assign -.-> lab-28329{{"`Max Array Value Based on Function`"}} javascript/spread_rest -.-> lab-28329{{"`Max Array Value Based on Function`"}} end

How to Find the Maximum Value of an Array Based on a Function

To find the maximum value of an array based on a function, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.
  2. Use Array.prototype.map() to map each element of the array to the value returned by the provided function, fn.
  3. Use Math.max() to get the maximum value of the mapped array.

Here's an example code snippet that implements the above steps:

const maxBy = (arr, fn) =>
  Math.max(...arr.map(typeof fn === "function" ? fn : (val) => val[fn]));

To use the maxBy function, pass in an array and the function that should be used to map each element to a value. You can either pass in a function directly or a string representing the key that should be used to access the value in each object of the array.

Here are some example calls to the maxBy function:

maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], (x) => x.n); // returns 8
maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], "n"); // returns 8

Summary

Congratulations! You have completed the Max Array Value Based on Function lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like