0

I store CQL filtering params as varchar in the database which i have to manipulate in JavaScript. street_desc is a variable in javascript. How can i assign it to the string dynamically?

Whatever is inside the input_params has to replace the cql_param string

var street_desc= "whatever"; //input from the user
var town_code = "whatever2"; //input from the user
var cql_param= "roadname_gr='street_desc'&towndesc is not null&town_code='town_code'"; //comes from database

var input_params = [street_desc,town_code]; //declare input params to replace the cql string

The output should be:

"roadname_gr='whatever'&towndesc is not null&town_code='whatever2'";

What i have tried...

var street_desc = "whatever"; //input from the user
var town_code = "whatever2"; //input from the user
var cql_param = "roadname_gr='street_desc'&towndesc is not null&town_code='town_code'"; //comes from database

var input_params = {
    street_desc: street_desc,
    town_code: town_code
}; //declare input params to replace the cql string



for (const [key, value] of Object.entries(input_params)) {
    console.log(`${key}: ${value}`);
    cql_param.replace(key, value);
}
csandreas1
  • 2,026
  • 1
  • 26
  • 48

2 Answers2

0

I update my answer to use a reducer

let cql_param= "roadname_gr='street_desc'&towndesc is not null&town_code=1"; 
var street_desc= "whatever"; //input from the user
var town_code = "whatever2"; //input from the user

var input_params = [street_desc,town_code]; 
var input_params_names = ["street_desc","1"]; 
let i =0;
const result = input_params.reduce((ac,c)=>{
  return ac.replace(input_params_names[i++], c);
},cql_param)

console.log(result)
سعيد
  • 1,547
  • 1
  • 14
  • 32
  • have you tried it I update its my code its not missing now – سعيد Oct 26 '20 at 16:29
  • output is still incorrect. It should be `"roadname_gr='whatever'&towndesc is not null&town_code='whatever2'";` – csandreas1 Oct 26 '20 at 16:39
  • are these the only values you want to replace or are there more meaning some other cases you didn't include ? – سعيد Oct 26 '20 at 16:42
  • these are just examples, now it seems correct. But i believe it seems a bit complicated. I will update my question with a simpler attempt i am trying at the moment. – csandreas1 Oct 26 '20 at 16:45
0

I managed to solve it. Here is what i did: Declare an object with my input params i want to replace. Iterate over the key-value pairs and replace the string.

var street_desc_inp = "whatever"; //input from the user
var town_code_inp = "whatever2"; //input from the user
var cql_param = "roadname_gr='street_desc_inp'&towndesc is not null&town_code='town_code_inp'"; //comes from database

console.log(cql_param)

var input_params = { //declare input params to replace the cql string
street_desc_inp: street_desc_inp,
town_code_inp: town_code_inp
};


for (const [key, value] of Object.entries(input_params))
cql_param = cql_param.replace(key, value);


console.log(cql_param)
csandreas1
  • 2,026
  • 1
  • 26
  • 48