I am using the query-string library to parse query parameters from an URL.
When the query parmater is in the form ?foo=bar
, the lib returns an object like this:
{
foo: bar
}
When in the form ?foo=bar1,bar2
, the object looks like this:
{
foo: [bar1, bar2]
}
I want to apply the function myFunction
on each element of myObject.foo
to obtain something like [myFunction(bar)]
or [myFunction(bar1), myFunction(bar2)]
Is there a way to do it easily with something like
myObject.foo.mapOrApply(myFunction)
without having to check if this is an array ?
For now, I am forced to do it this way and I find this very unaesthetic:
Array.isArray(myObject.foo)
? myObject.foo.map(myFunction)
: [myFunction(myObject.foo)];