Nest all content into snippets

This commit is contained in:
Angelos Chalaris
2023-05-07 16:07:29 +03:00
parent 2ecadbada9
commit 6a45d2ec07
1240 changed files with 0 additions and 0 deletions

27
snippets/js/s/rearg.md Normal file
View File

@ -0,0 +1,27 @@
---
title: Rearrange function arguments
type: snippet
language: javascript
tags: [function]
cover: island-corridor
dateModified: 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`.
```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']
```