Zacharyzac
Zacharyzac

Reputation: 23

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?

Upvotes: 2

Views: 702

Answers (1)

Rafael
Rafael

Reputation: 7746

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 = {};
o.a = o;
console.log(JSON.stringify(o));

Upvotes: 2

Related Questions