_.get(params, "_q")
can output either a string or an array of strings.
In my current code, when I input "hello, there", it translates to an array of strings and results in an error saying
h.get(...).trim is not a function
.
Please take a look at my current code below and what I have attempted so far. Is my attempted solution sufficient, or is there a more modern JS approach?
Note: _
refers to lodash.
Result of console.log(params) for "hello, there"
{
"_q": [
"hello",
" there "
]
}
Result of console.log(params) for "hello"
{
"_q": "hello"
}
Result of console.log(params) for "hello there"
{
"_q": "hello there"
}
Current code
let where = {};
if (_.get(params, "_q")) {
where._q = _.get(params, "_q").trim();
}
Fixes I have attempted
const _q = _.get(params, "_q");
if (_q) {
where._q = []
.concat(_q)
.map((str) => str.split(",").map((str) => str.trim()))
.flat();
}