Files
30-seconds-of-code/snippets/rearg.md
Isabelle Viktoria Maciohsek 8bf67754ce Make expertise a field
2022-03-01 20:21:45 +02:00

655 B

title, tags, expertise, firstSeen, lastUpdated
title tags expertise firstSeen lastUpdated
Rearrange function arguments function intermediate 2018-01-28T15:04:21+02:00 2020-10-22T20:24:04+03:00

Creates a function that invokes the provided function with its arguments arranged according to the specified indexes.

  • Use Array.prototype.map() to reorder arguments based on indexes.
  • Use the spread operator (...) to pass the transformed arguments to fn.
const rearg = (fn, indexes) => (...args) => fn(...indexes.map(i => args[i]));
var rearged = rearg(
  function(a, b, c) {
    return [a, b, c];
  },
  [2, 0, 1]
);
rearged('b', 'c', 'a'); // ['a', 'b', 'c']