Files
30-seconds-of-code/snippets/rearg.md
Angelos Chalaris fc625b6229 Code styling
2018-08-13 14:25:31 +03:00

20 lines
507 B
Markdown

### rearg
Creates a function that invokes the provided function with its arguments arranged according to the specified indexes.
Use `Array.map()` to reorder arguments based on `indexes` in combination with the spread operator (`...`) to pass the transformed arguments to `fn`.
```js
const rearg = (fn, indexes) => (...args) => fn(...indexes.map(i => args[i]));
```
```js
var rearged = rearg(
function(a, b, c) {
return [a, b, c];
},
[2, 0, 1]
);
rearged('b', 'c', 'a'); // ['a', 'b', 'c']
```