Single Quote and Double Quote Append

Viewed 1112

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'"
5 Answers

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)

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.

Related