In Clojure destructuring it's possible to also have a binding for the full array using :as
, is something similar possible in Javascript?
For example,
const queue = [[1, 2]];
const [x, y :as pos] = queue.shift();
where x
would be 1, y
would be 2 and pos
would be [1, 2]
?
or is an extra in-between step necessary, like
const queue = [[1, 2]];
const pos = queue.shift();
const [x, y] = pos;
CodePudding user response:
You can't get the parent and nested properties at the same time during destructuring.
I'd do it separately for readability
const queue = [ [1, 2] ],
pos = queue.shift(),
[x, y] = pos;
BUT, it is possible to do it in single line. If you destructure the array like an object. Get the 0
property to a variable and destructure the nested array
const queue = [ [1, 2] ];
const { 0: pos, 0: [x, y] } = queue
console.log(x, y)
console.log(pos)