casillas
casillas

Reputation: 16813

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

  "id": " + ' + jsonObject["school_id"] + ' + "

expected output would be something similar as follows

"id" : "'12345'"

Upvotes: 4

Views: 2038

Answers (5)

Mr world wide
Mr world wide

Reputation: 4814

 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.

Upvotes: 1

Arpit Goyal
Arpit Goyal

Reputation: 1005

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

Upvotes: 3

toskv
toskv

Reputation: 31612

You could use template strings to easily create it.

let jsonObject = {school_id:"1234"}
let s = `"id" : "'${jsonObject["school_id"]}'"`;
console.log(s)

Upvotes: 3

sjahan
sjahan

Reputation: 5960

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);

Upvotes: 5

qiAlex
qiAlex

Reputation: 4346

please try this to achieve expected result "id" : "'12345'"

var jsonObject = {school_id: 12345}

var a = {"id": '\'' + jsonObject['school_id'] + '\''}

console.log (a)

Upvotes: 2

Related Questions