I want to get the last element from a generated array in a single line, is there a better way to do it?
I was thinking assign the array to foo
then immediatly assign the last element of foo
to itself again but it doesn't work:
function* getArray() {
yield* [1, 2, 3];
}
let foo = (foo = [...getArray()], foo[foo.length - 1]);
console.log(foo); // 3?
This works but it's calling getArray()
twice which is quite unnecessary.
function* getArray() {
yield* [1, 2, 3];
}
let foo = [...getArray()][[...getArray()].length - 1];
console.log(foo); // 3