An update on rest operator ?

Siegfried Ehret siegfried at ehret.me
Wed Aug 2 09:58:16 UTC 2017


Hello,

This is a small idea I had recently: to update the rest operator to make
it work not only to get the last elements.

As an example, we already have:

```javascript
const myArray = [1, 2, 3, 4, 5, ..., 99, 100];
[first, second, ...rest] = myArray;

// first = 1
// second = 2
// rest = [3, 4, 5, ..., 99, 100]
```

It would be interesting to have:

```javascript
[...rest, previous, last] = myArray;

// rest = [1, 2, 3, ..., 97, 98]
// previous = 99
// last = 100
```

And:

```javascript
[first, ...rest, last] = myArray;

// first = 1
// rest = [2, 3, ..., 97, 98, 99]
// last = 100
```

Another use case: inside a variadic function to separate the callback
(often the last argument) and the function parameters.

I asked myself «why would I want the last elements of an array», and my
answer was «for the same reason which motivates me to get the first
ones».

What do you think ?

-- 
  Siegfried Ehret
  siegfried at ehret.me
  +33 (0) 6 22 68 17 44


More information about the es-discuss mailing list