简体   繁体   English

JavaScript对象字符串/引用问题

[英]Javascript object string/quotes issue

I have this piece of code: 我有这段代码:

var selectdata = {
     "tablename": "dbo.vw_bla",
     "where": "ID_Prod like @idprod",
     "whereparameters": [{
         "name": "idprod",
         "value": ' +  $(prodvariable) + ',
         "type": "varchar"
     }],
     "orderby": "ID_Prod ASC"
 };

prodvariable is a variable in which a have stored a number. prodvariable是a中已存储数字的变量。 For example 6 . 例如6

This should be passed as 6 . 这应该作为6传递。

I have tried several combinations of quotes, escapes, +, etc but I cannot figure out the right way. 我尝试了引号,转义,+等的几种组合,但是我找不到正确的方法。

If you want to pass the prodvariable as number try like below 如果您想将prodvariable作为数字传递,请尝试如下所示

var prodvariable = 6
var selectdata = {
     "tablename": "dbo.vw_bla",
     "where": "ID_Prod like @idprod",
     "whereparameters": [{
         "name": "idprod",
         "value": prodvariable,
         "type": "varchar"
     }],
     "orderby": "ID_Prod ASC"
 };

If you want to pass the variable as string try like below 如果您想将变量作为字符串传递,请尝试如下所示

var prodvariable = 6
var selectdata = {
     "tablename": "dbo.vw_bla",
     "where": "ID_Prod like @idprod",
     "whereparameters": [{
         "name": "idprod",
         "value": `${prodvariable}`, 
         "type": "varchar"
     }],
     "orderby": "ID_Prod ASC"
 };

You could do one of the following method to pass prodvariable to json object. 您可以执行以下方法之一,以将prodvariable传递给json对象。

"value": prodvariable // this will be passed as number if prodvariable is number “值”:prodvariable //如果prodvariable为数字,则将作为数字传递

"value": ${prodvariable} , “值”: ${prodvariable}

OR 要么

"value": '"'+ prodvariable + '"', “ value”:'“'+ prodvariable +'”',

You do not need any quotes for prodvariable . prodvariable不需要任何引号。 Use that variable directly in the value: 直接在值中使用该变量:

 var prodvariable = '6'; var selectdata = {"tablename":"dbo.vw_bla", "where":"ID_Prod like @idprod", "whereparameters": [ {"name": "idprod", "value": prodvariable , "type":"varchar"}], "orderby":"ID_Prod ASC"}; console.log(selectdata); 

If I understand correctly, you want to use the value of the prodvariable variable but surrounded with double quotes? 如果我理解正确,您是否想使用prodvariable变量的值,但用双引号将其括起来? You can do so using ES2015 string template literals: 您可以使用ES2015字符串模板文字来实现:

{ "value":  `"${prodvariable}"` }

Then value will be "6" in your case. 然后,在您的情况下, value将为"6"

You can achieve the same result with simple string concatenation as well: 您也可以通过简单的字符串连接来达到相同的结果:

{ "value":  '"' + prodvariable + '"' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM