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"
    }]
}
4

4 回答 4

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/"
    }]
}
于 2013-05-09T22:42:15.150 回答
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
    }];
于 2013-05-09T22:42:15.180 回答
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"
    }]
}
于 2013-05-09T22:42:47.510 回答
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.

于 2013-05-09T22:43:29.340 回答