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);
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.
Any updates?
Most helpful comment
See leobalters version, if your usecase needs
pushyou can use: