_.get(params, "_q")
outputs either string or array of strings.
On my current code, when I type for example "hello, there", it translates to array of strings and as a result is says something like h.get(...).trim is not a function
.
Pls see my current code below. and what i've tried so far. Is my tried code good or is there a better way using modern JS?
Note: _
is from lodash.
Result of console.log(params) when typing "hello, there"
{
"_q": [
"hello",
" there "
]
}
Result of console.log(params) when typing "hello"
{
"_q": "hello"
}
Result of console.log(params) when typing "hello there"
{
"_q": "hello there"
}
Current code
let where = {};
if (_.get(params, "_q")) {
where._q = _.get(params, "_q").trim();
}
What fix I have tried so far
const _q = _.get(params, "_q");
if (_q) {
where._q = []
.concat(_q)
.map((str) => str.split(",").map((str) => str.trim()))
.flat();
}
lodash.get({_q: 'hello, there'}, '_q')
returns a string"hello, there"
, not an array. runkit.com/embed/5gpphlg8c9d2 Can't reproduce your issue. So please clarify the input and output