0

_.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();
  }
8
  • 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
    – Lin Du
    Commented Jul 31, 2023 at 9:29
  • @PeterSeliger The "What fix I have tried so far" in my question works but not sure if that is the ideal way to do it
    – Joseph
    Commented Jul 31, 2023 at 10:30
  • @LinDu. I have updated my question with some examples
    – Joseph
    Commented Jul 31, 2023 at 12:43
  • Is this an XY Problem? The title appears to be about searching strings (or arrays of strings), but the body content looks to be about Lodash functionality. What are you really trying to accomplish here?
    – Drew Reese
    Commented Jul 31, 2023 at 16:22
  • @DrewReese. The result is either strings or array of strings. With my current code, when the output is array of strings, this issues occurs h.get(...).trim is not a function
    – Joseph
    Commented Jul 31, 2023 at 16:31

1 Answer 1

0

I see, the "input", e.g. the "_q" property value can be either a single string, or an array of strings.

I'd suggest plopping params._q into an array and flatten it first, then map the strings in the array to a new array of trimmed string values.

Example:

[get(params, "_q")].flat().map((el) => el.trim())

Given:

  • Array of strings

    const params = {
      _q: ["hello", " there "]
    };
    
    [get(params, "_q")]       // [["hello", " there "]]
      .flat()                 // ["hello", " there "]
      .map((el) => el.trim()) // ["hello", "there"]
    
  • String

    const params = {
      _q: " hello there "
    };
    
    [get(params, "_q")]       // [" hello there "]
      .flat()                 // [" hello there "]
      .map((el) => el.trim()) // ["hello there"]
    

Demo

const params = {
  _q: ["hello", " there "]
};

console.log([params._q].flat().map((el) => el.trim())); // ["hello", "there"]

3
  • Hi Drew. How can I output this as a string always instead of array?
    – Joseph
    Commented Aug 2, 2023 at 9:13
  • @Joseph You can join the array which returns a string.
    – Drew Reese
    Commented Aug 2, 2023 at 9:20
  • @Joseph What exactly are you trying to do now? You want your function to always result in a string instead of an array? Even if the input is an array of strings, you want them all trimmed and then "joined" altogether as a single string?
    – Drew Reese
    Commented Aug 2, 2023 at 15:14

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.