简体   繁体   中英

Javascript Object internal self referencing

Is there a way to reference another portion of a JSON object inside of the same JSON object?

I have an object below that makes a reference in the "MapParameters" object to the "home" object.

{
"parameters": {
    "data": {
        "URL": "http://SC.json",
        "name": "SC"
    },
    "MapParameters": {
        "center": [home.lat, home.lng],
        "zoom": home.zoom,
        layers: [streets, layers]
    },
    "basemap": {
        "basemapsText": {
            "<span class='pointer'>Streets</span>": "streets",
            "<span class='pointer'>Satellite</span>": "aerial"
        }
    "other": {
        "scale": {
            "maxWidth": 200,
            "metric": true,
            "imperial": true
        },
        "home": {
            lat: 37.26,
            lng: -93.53,
            zoom: 7
        }
    }
}

Is this possible, and if so, how is it written?

No, you cannot have circular references in JSON, but you can have circular references in JavaScript objects. The reason this is, is because it's not serializable:

 let o = {}; oa = o; console.log(JSON.stringify(o)); 

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