Home > Software design >  How can i convert comma separated query String to a Array of Strings
How can i convert comma separated query String to a Array of Strings

Time:08-17

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

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)
      }
   }

CodePudding user response:

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)

      }



    }

CodePudding user response:

SQL requires quotes around each string in the IN list, and also requires it to be inside ().

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

if (propertytype) {
  console.log('we have Property Filter')
  if (propertytype.length > 0) {
    let inPropType = propertytype.split(",")
    let inPropTyperray = inPropType.map(prop => `'${prop}'`).join(',')

    myQstr.push(`LOWER(f.Record.propertyType) IN (${inPropTyperray})`);

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

CodePudding user response:

It's pretty simple. You just need to split the string with the delimiter/separator , and you should be getting an array of strings.

const propertyType = 'sfr,condo,vac';
const myQstr = propertyType.split(",");

console.log(myQstr); // [ 'sfr', 'condo', 'vac' ]
  • Related