0

I am using query string (https://www.npmjs.com/package/qs) to parse the params in the URL like so:

http://www.website.com/?filter="bags"&filter="shirts"

However, if I have no filter or just one filter it will return either a string or undefined:

http://www.website.com/?filter="bags" // returns 'bags'
http://www.website.com/ // returns undefined

The reason being I need to feed this result into functions / methods that use an array.

One solution I thought of is to sort of create condition depending on what it returns:

if (typeof query == 'string') {
    query = [query];
} else if (query == undefined) {
    query = [];
}

But this seems not optimal. Wondering if the result can just be an array all time instead.

0

7 Answers 7

1

I think there is nothing much you can do. Though, you can use ternary operator to reduce some code. The following will return an empty array if query does not have anything:

if(!Array.isArray(query))
  query = !!query ? [query] : [];
Sign up to request clarification or add additional context in comments.

7 Comments

You edited your answer exactly like mine but what is the purpose of posting the same answer?
@soroushchehresa, the same I could say, like I have used ternary operator even before you answered......more over, without any explanation what the code is doing how the answer could help someone!!!
Maybe it can be a reason but not compelling!
This solution seems to wrap the array into another array. I think coz the first condition satisfies both string and array.. so it wraps the array into another array.
@catandmouse, you are right. Just wanted to know, in any case, could query be an array before modifying?
|
1

The reason being I need to feed this result into functions / methods that use an array.

i think the better solution is instead of checking query is array orundefined, you can add default param of function/method to be array

example

function yourFunction(query = []){
    return query;
}

2 Comments

so what if it's a string or an integer?
@vol7ron don't worry qs parse will return either a string or undefined.
1

I think you could do:

query = !!query ? Array.isArray(query) ? query : [query] : [];

or:

if(!!query) {
  if(!Array.isArray(query)) {
    query = [query];
  }
} else {
  query = [];
}

Comments

0

Do this way, if condition checks query is undefined or not.

if (query) {
   query = [query];
} else {
   query = [];
}

Comments

0

You could do something like this:

var filter = [];

if(query) {
  filter.push(query);
}

This way, filter would always be an array and contain the query filters, if provided. This pattern is not uncommon.

Comments

0

I would initialize an empty query. Suppose, your query is stored in state, then:

const { query } = this.state || { query: [] }

Or, simply initialize the query with empty array in the state:

state = {
  query: [] // rather than ''
}

Now, you should only set the query state when it has value inside your handler:

const query = e.target.value // or whatever the source you get the query from
if(query) {
  this.setState({query: [query]})
}

Comments

0

Another option in addition to the many existing answers it that you could always wrap it and then filter out the undefineds:

let str = 'str';
console.log( [str].filter(v=>v!==undefined) )   // [ 'str' ]

let undf;
console.log( [undf].filter(v=>v!==undefined) )  // [ ]

// Example: filter out undefined
query = [query].filter(v=>v!==undefined)

// Example: passing in a Boolean constructor, since non-empty strings are truthy
query = [query].filter(Boolean)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.