Ecma262: Spread operator within ternary operator

Created on 16 Mar 2016  路  10Comments  路  Source: tc39/ecma262

I think it would be nice to be able to use spread operator like this:

var x = [1,2];
var y = [];
y.push(x instanceof Array ? ...x : x);

Most helpful comment

See leobalters version, if your usecase needs push you can use:

var x = [1,2];
var y = [];
y.push(...Array.isArray(x) ? x : [ x ]);

All 10 comments

Feature proposal discussions belong on es-discuss.

var x = [1,2];
var y = [];
y.concat(x);

See leobalters version, if your usecase needs push you can use:

var x = [1,2];
var y = [];
y.push(...Array.isArray(x) ? x : [ x ]);

@leobalter Very nice suggestion if I don't mind creating a new array.

@topaxi It looks like a workaround rather than an official method for the case. Also, I personally think it's a bit less readable.

Not the appropriate venue for this discussion.

Excuse my necrophilia, but wouldn't that do the trick?:

let x = [1,2];
let y = [];
y.push(...(x instanceof Array ? x : [x]));

@arielgee that鈥檚 just a less robust, not-cross-realm form of https://github.com/tc39/ecma262/issues/478#issuecomment-197373487.

Hello there,

Feature proposal discussions belong on es-discuss.

Did someone did it ?

Thanks

Any updates?

Was this page helpful?
0 / 5 - 0 ratings