admin管理员组文章数量:1314215
I am trying to append a single quote and also add double quote, but it shows an error as follows
[ts] ':' expected
"id": " + ' + jsonObject["school_id"] + ' + "
expected output would be something similar as follows
"id" : "'12345'"
I am trying to append a single quote and also add double quote, but it shows an error as follows
[ts] ':' expected
"id": " + ' + jsonObject["school_id"] + ' + "
expected output would be something similar as follows
"id" : "'12345'"
Share
Improve this question
edited Oct 31, 2018 at 16:23
casillas
asked Oct 31, 2018 at 16:18
casillascasillas
16.8k21 gold badges123 silver badges226 bronze badges
3
- why do u want to put it double quotations again.? its string already.! – Mr world wide Commented Oct 31, 2018 at 16:22
- It is requirement from service side to handle one posite key. I do not know in depth. – casillas Commented Oct 31, 2018 at 16:23
-
The word for the character is "quote", not "quota". I had edited, but your later edit replaced mine. You need to match your quotes; Please show the full code, as it is not clear where you are starting from. Also note that
JSON.stringify
might be a better route. – Heretic Monkey Commented Oct 31, 2018 at 16:23
5 Answers
Reset to default 5You 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
please try this to achieve expected result "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.
本文标签: javascriptSingle Quote and Double Quote AppendStack Overflow
版权声明:本文标题:javascript - Single Quote and Double Quote Append - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741923402a2405140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论