Rearrange Function Arguments

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore the rearg function in JavaScript, which allows us to rearrange the order of arguments passed to a function based on specified indexes. We will learn how to use the Array.prototype.map() method and spread operator to transform the arguments and invoke the original function with the rearranged arguments. By the end of this lab, you will have a good understanding of how to use the rearg function to manipulate function arguments in JavaScript.


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/functions("`Functions`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/closures("`Closures`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28577{{"`Rearrange Function Arguments`"}} javascript/data_types -.-> lab-28577{{"`Rearrange Function Arguments`"}} javascript/arith_ops -.-> lab-28577{{"`Rearrange Function Arguments`"}} javascript/comp_ops -.-> lab-28577{{"`Rearrange Function Arguments`"}} javascript/functions -.-> lab-28577{{"`Rearrange Function Arguments`"}} javascript/array_methods -.-> lab-28577{{"`Rearrange Function Arguments`"}} javascript/closures -.-> lab-28577{{"`Rearrange Function Arguments`"}} javascript/higher_funcs -.-> lab-28577{{"`Rearrange Function Arguments`"}} javascript/spread_rest -.-> lab-28577{{"`Rearrange Function Arguments`"}} end

How to Rearrange Function Arguments in JavaScript

To rearrange function arguments in JavaScript, you can use the rearg() function. First, create a function that invokes the provided function with its arguments arranged according to the specified indexes. You can use Array.prototype.map() to reorder arguments based on indexes. Then, use the spread operator (...) to pass the transformed arguments to fn.

Here's an example of how to use the rearg() function:

const rearg =
  (fn, indexes) =>
  (...args) =>
    fn(...indexes.map((i) => args[i]));

In this example, we use rearg() to create a new function that rearranges the arguments of another function.

var rearged = rearg(
  function (a, b, c) {
    return [a, b, c];
  },
  [2, 0, 1]
);
rearged("b", "c", "a"); // ['a', 'b', 'c']

In the code above, we create a new function rearged that rearranges the arguments of the function function(a, b, c) { return [a, b, c]; }. The indexes argument specifies the order in which the arguments should be rearranged. In this case, we want the third argument to come first, the first argument to come second, and the second argument to come third. When we call rearged('b', 'c', 'a'), it returns ['a', 'b', 'c'], which is the result of calling the original function with the rearranged arguments.

Summary

Congratulations! You have completed the Rearrange Function Arguments lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like