Desestructuración para obtener el último elemento de una matriz en es6
En coffeescript esto es sencillo:
coffee> a = ['a', 'b', 'program']
[ 'a', 'b', 'program' ]
coffee> [_..., b] = a
[ 'a', 'b', 'program' ]
coffee> b
'program'
¿Es6 permite algo similar?
> const [, b] = [1, 2, 3]
'use strict'
> b // it got the second element, not the last one!
2
> const [...butLast, last] = [1, 2, 3]
SyntaxError: repl: Unexpected token (1:17)
> 1 | const [...butLast, last] = [1, 2, 3]
| ^
at Parser.pp.raise (C:\Users\user\AppData\Roaming\npm\node_modules\babel\node_modules\babel-core\node_modules\babylon\lib\parser\location.js:24:13)
Por supuesto que puedo hacerlo de la manera es5 -
const a = b[b.length - 1]
Pero tal vez esto es un poco propenso a errores off by one. ¿Puede el splat solo ser la última cosa en la desestructuración?
5 answers
No es posible en ES6/2015. El estándar simplemente no lo prevé.
Como puedes ver en la especificación , el FormalParameterList
puede ser:
- a
FunctionRestParameter
- a
FormalsList
(una lista de parametos) - a
FormalsList
, seguido de aFunctionRestParameter
Con FunctionRestParameter
seguido de parámetros no se proporciona.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-16 19:21:30
const [last] = [1, 3, 4, 5].slice(-1)
const [second_to_last] = [1, 3, 4, 5].slice(-2)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-19 08:56:02
Creo que ES6 podría al menos ayudar con eso:
[...arr].pop()
Dado que su matriz (arr) no es indefinida y un elemento iterable (sí, incluso las cadenas funcionan!!), debe devolver el último elemento..incluso para el array vacío y tampoco lo altera. Sin embargo, crea una matriz intermedia..pero eso no debería costar mucho.
Su ejemplo se vería así:
[...['a', 'b', 'program']].pop() -> 'program'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-25 16:32:56
Puede desestructurar la matriz invertida para acercarse a lo que desea.
const [a, ...rest] = ['a', 'b', 'program'].reverse();
document.body.innerHTML =
"<pre>"
+ "a: " + JSON.stringify(a) + "\n\n"
+ "rest: " + JSON.stringify(rest.reverse())
+ "</pre>";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-24 11:57:30
No necesariamente la forma más performante de hacer. Pero dependiendo del contexto, una forma bastante elegante sería:
const myArray = ['one','two','three'];
const theOneIWant = [...myArray].pop();
console.log(theOneIWant); // 'three'
console.log(myArray.length); //3
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-01 01:36:09