0

I have a JSON object in which I am having a RAW SQL query with a parameter inside it. I am getting the value of the parameter from my HTML DOM element. HOW to replace it can any one tell me? thanks

EG:-

[1,2,"select * from animal where animal = @value"]

i want to replace @value

4
  • in which environment do you need the replacement? Commented Jun 21, 2017 at 7:10
  • So will it be single where condition always? Commented Jun 21, 2017 at 7:10
  • @NinaScholz i want to replace in jquery Commented Jun 21, 2017 at 7:12
  • @GuruprasadRao yes Commented Jun 21, 2017 at 7:12

2 Answers 2

3

If thats a string you can always use regex.

let jsonValue = [1,2,"select * from animal where animal = @value"]
jsonValue[2] = jsonValue[2].replace(/@value/, 'tiger'); // output select * from animal where animal = tiger
Sign up to request clarification or add additional context in comments.

5 Comments

yes sure, you can replace the @value with whatever you want, instead of using tiger you can use "tiger"
i am getting actual value like "[[1,2,"select * from animal where animal = @value"]]" . Now i need to replace @value with some parameter.
yes of course, replace is not replacing the actual value but returning a new string. Take a look to the new code
@nicowernli i am working on offline system it is showing cannot read property 'replace' of undefined
Well that means that jsonValue[2] does not exist. I've use the number 2 because was the index in the example you posted, but you should use the corresponding index of the string you want to change
1

If you include the Lodash library in you project you can use this method, otherwise you need to use the traditional javascript replace method.

_.replace(array[2], '@value', 'my new value');

Comments

Your Answer

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