0

What's the best way to reference Javascript Object that exists inside of another Javascript object? For instance, taking the data below, how would I properly refer to the "home" of an individual, which is a reference to another object (places) in the same dataset?

var data = {
    "people": [{
        "name": "Jack",
        "age": "8",
        "home": data.places[0].country
    }, {
        "name": "John",
        "age": "9",
        "home": data.places[1].country
    }],
    "places": [{
        "country": "Holland"
    }, {
        "country": "Germany"
    }]
}
bender
  • 5
  • 5

4 Answers4

0

Generally, JSON is used as a "nested" data format. So, instead of relational pointers to other pieces of data (as in relational databases), the data is simply inserted directly as a sub-object. In practice, this results in some data duplication (denormalization, in database terms).

{
    "people": [{
        "name": "Jack",
        "age": "8",
        "home": "Holland"
    }, {
        "name": "John",
        "age": "9",
        "home": "Germany"
    }],
    "places": [{
        "country": "Holland"
    }, {
        "country": "Germany"
    }]
}

The closest thing to "references" that I've seen with standard JSON is when it is used in an API (as in Tastypie). In this setup, the references are encoded as API URIs that can later be used by client to request the other bits of the dataset.

{
    "people": [{
        "name": "Jack",
        "age": "8",
        "home": "/api/v1/place/1/"
    }, {
        "name": "John",
        "age": "9",
        "home": "/api/v1/place/2/"
    }]
}
voithos
  • 68,482
  • 12
  • 101
  • 116
  • Since it's not JSON, the OP has probably hand-written this and *does* need normalisation for simple updating. But if would solve the problem, yes, and even would be shorter :-) – Bergi May 09 '13 at 22:49
0

Why not reformat your data like so:

var data.places = {
    "Holland": {otherinfo},
    "Germany": {otherinfo}
};

data.people = [{
        "name": "Jack",
        "age": "8",
        "home": data.places.Holland
    }, {
        "name": "John",
        "age": "9",
        "home": data.places.Germany
    }];
Jay
  • 3,471
  • 4
  • 35
  • 49
0

I would solve the problem by listing all of the places, assigning each an ID.

Then each person would simply reference the ID.

var data = {
    "people": [{
        "name": "Jack",
        "age": "8",
        "home_id": 0
    }, {
        "name": "John",
        "age": "9",
        "home_id": 1
    }, {
        "name": "Inge",
        "age": "11",
        "home_id": 0
    }],
    "places": [{
        "id" : 0,
        "country": "Holland"
    }, {
        "id" : 1,
        "country": "Germany"
    }]
}
Andrew Shepherd
  • 44,254
  • 30
  • 139
  • 205
0

Split up your declaration so that data.places will exist when you reference it.

var data = {
    "places": [{
        "country": "Holland"
    }, {
        "country": "Germany"
    }]
}

// data is now defined and has the property "places"

data.people = [{
    "name": "Jack",
    "age": "8",
    "home": data.places[0].country // No more error 
}, {
    "name": "John",
    "age": "9",
    "home": data.places[1].country // No more error either
}]

In your question, your object was referencing elements on itself before it had been fully defined. This resulted in a TypeError: Cannot read property 'places' of undefined. In my solution, I broke apart your object definition so that when you define data.people the data.places piece array already exists.

Objects don't have to be declared all at once.

Mike Robinson
  • 24,971
  • 8
  • 61
  • 83