Is it possible to do this:
const foo = [1, 2];
const bar = {
a: foo[0],
b: foo[1],
c: 'something else not in array'
};
as a single statement, avoiding the need to declare foo
?
As example, the array could be the result of
"1, 2".split(', ')
and you want to avoid the interim variable declaration, using "1"
and "2"
as the values for two of the properties (and potentially not the only properties) in a new object.
I can imagine something like this, though neither is valid for various reasons:
const bar { a, b, c: 'something else not in array' } = [1, 2];
or
const bar { a:[0], b:[1], c: 'something else not in array' } = [1, 2];
EDIT: The closest I've found, without using an IIFE, is
Object.assign({c: 'something else not in array'}, [1, 2])
which has a negative in that rather than properties named 'a' and 'b', you get properties named '0' and '1':
{0: 1, 1: 2, c: "something else not in array"}