It's not really a problem, but I like to improve my JavaScript/ES6 skills.
I'm trying to find a more efficient way to always have an array, no matter if my incoming variable has just a single value or an array.
What I'm currently doing is:
var inp;
// just for the showcase
if (Math.random() > 0.5) {
inp = 'foo';
} else {
inp = ['foo', 'bar'];
}
// this is what I want to optimize
if (Array.isArray(inp)) {
outp = inp;
} else {
outp = [inp];
}
console.log(outp);
I'm not talking just shortening the code at any price, but effiency and maybe elegance.
