Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i convert comma separated query String to a Array of Strings

I am wondering if there is a

i have a query param which I get when user calls a URL and that can have one or more values which ill be comma separated. When I get the param I need to convert the comma separated list of items into array of strings, so I can pass them to my SQL query. The below code
prints out the [ ‘sfr’, ‘condo’, ‘vac’ ] for the inPropTyperray but then when i add the array to myQstr it no longer is a array of string but a list comma separated like this.
[ ‘LOWER(f.Record.propertyType) IN sfr,condo,vac’ ]

Not sure what i am missing

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

propertytype = 'sfr,condo,vac'
myQstr =[]



    if(propertytype){
      console.log('we have Property Filter')
      if(propertytype.length > 0){
        let inPropType = propertytype.split(",")
        let inPropTyperray = []
        inPropType.forEach(function(element){
        inPropTyperray.push(element)
        }) 

        myQstr.push("LOWER(f.Record.propertyType) IN " + inPropTyperray )

        console.log(inPropTyperray)
        console.log(myQstr)

      }



    }
propertytype = 'sfr,condo,vac'
myQstr =[]

  if(propertytype){
      console.log('we have Property Filter')
      if(propertytype.length > 0){
        let inPropType = propertytype.split(",")
        let inPropTyperray = []
        inPropType.forEach(function(element){
        inPropTyperray.push(element)
        }) 
        myQstr.push("LOWER(f.Record.propertyType) IN " + inPropTyperray )
        console.log(inPropTyperray)
        console.log(myQstr)
      }
   }

>Solution :

You should map array values to SQL strings and add those brackets to query string:

propertytype = 'sfr,condo,vac'
myQstr =[]



    if(propertytype){
      console.log('we have Property Filter')
      if(propertytype.length > 0){
        let inPropType = propertytype.split(",")
        let inPropTyperray = []
        inPropType.forEach(function(element){
        inPropTyperray.push(element)
        }) 

        myQstr.push(`LOWER(f.Record.propertyType) IN (${inPropTyperray.map(i => `'${i}'`)})`)

        console.log(inPropTyperray)
        console.log(myQstr)

      }



    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading