I wrote a function which has to support two types of a paramter names for a list of values. Internally it deals with the parameter as an array.
A single name is given as string and multiples names are given as an array of strings.
// simplified example
let doSome = names => names.map(name => name.toUpperCase())
names(['Bart', 'Lisa'])
// [ 'BART', 'LISA' ]
names('Homer')
// TypeError: names.map is not a function
I found a solution using Array.of() in combination with flatten() which needs some babel configuration.
doSome = names => Array.of(names).flatten().map(name => name.toUpperCase());
Is there an idiomatic way in JavaScript to get an array without a type check?


(names instanceof Array ? names : [names]).map()?typeof names === 'object' && names instanceof Array. Typeof returns a set of string values corresponding to the basic JS types, and "instanceof" checks some prototypical inheritance stuff. These answers are neat one-liners for golfing, but it's more maintainable to just explicitly type-check rather than to get clever.