[英]Single Quote and Double Quote Append
I am trying to append a single quote and also add double quote, but it shows an error as follows 我试图附加单引号并添加双引号,但它显示如下错误
[ts] ':' expected
[ts]':'预计
"id": " + ' + jsonObject["school_id"] + ' + "
expected output would be something similar as follows 预期产出将类似于以下内容
"id" : "'12345'"
You can't just use '
like that. 你不能只用
'
这样的。
If you want to include single quotes in the string, enclose them in a string. 如果要在字符串中包含单引号,请将它们包含在字符串中。
const jsonObject = {"school_id": "12345"} const obj = {"id": "'" + jsonObject["school_id"] + "'"} console.log(obj);
You could use template strings to easily create it. 您可以使用模板字符串轻松创建它。
let jsonObject = {school_id:"1234"} let s = `"id" : "'${jsonObject["school_id"]}'"`; console.log(s)
You can simply use Template Strings: 您只需使用模板字符串:
const obj = { school_id: "1234" }
const str = `"id" : "'${obj["school_id"]}'"`;
I have not worked much with TypeScript, but looks like they are supported: https://basarat.gitbooks.io/typescript/docs/template-strings.html 我没有使用TypeScript,但看起来支持它们: https : //basarat.gitbooks.io/typescript/docs/template-strings.html
please try this to achieve expected result "id" : "'12345'"
请尝试这个以达到预期的结果
"id" : "'12345'"
var jsonObject = {school_id: 12345} var a = {"id": '\\'' + jsonObject['school_id'] + '\\''} console.log (a)
var school_id = '12345'; school_id = "'"+school_id+"'"; school_id = '"'+school_id+'"'; console.log(school_id);
You can do like this but make sure you know the use before doing it. 你可以这样做,但在做之前一定要知道用途。 don't code blindly.
不要盲目编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.