简体   繁体   中英

How to concat double quotes string inside JSON object

#Below shows function with JSON object inside #

function xyz(field)
{

let object = {

a : { 

 name : _ABC_. + field 

    }

return object;

}

**Output expected **

'" ABC "."field"' Where field is equal to value of input parameter passed

Your question is not entirely clear. Assuming for input XYZ the output expected is "ABC"."XYZ" , this would be the code. Double quotes can be placed in the Template string

function xyz(field) {
    let object = {
        a: {
           name: `"ABC"."${field}"`
        }
    };
    return object;
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

If you have to work with ES5 and below you could also escape the " with a backslash as follows:

function xyz(field) {
    let object = {
        a: {
           name: "\"ABC\"" + ".\"" + field + "\""
        }
    };
    return object;
}

Try this:

 function xyz(field) { var object = { a: { name: { ABC: { field: field } } } } return object } console.log(xyz("String")) console.log(xyz("String").a.name.ABC.field)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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