12

I'm watching out for a shortcut way to use values from a dictionary as an internal reference inside the dictionary. The code shows what I mean:

var dict = {
    'entrance':{
        'rate1': 5,
        'rate2':10,
        'rate3':20,
    },

    'movies':{
        'theDarkKnight':{
            '00:00':<entrance.rate1>,
            '18:00':<entrance.rate2>,
            '21:00':<entrance.rate3>
        },
        ...
    };

is there a sneaky way to do this?

Teemu
  • 22,918
  • 7
  • 53
  • 106
Dominik H
  • 303
  • 2
  • 6
  • 15
  • 3
    Unfortunately, JSON doesn't support recursion/self-referencing. – Alvin Wong Dec 03 '12 at 15:18
  • This isn't JSON. Are you asking about self-references while creating an object using literal syntax? Or are you saying that you want the specified values to update automatically with changes to the referenced values? Since it's not JSON, you could use functions or property getters to get the current values of other properties. – I Hate Lazy Dec 03 '12 at 15:19
  • @AlvinWong: "JSON" --> "JavaScript Object Notation", so: JSON = JavasScript. – Cerbrus Dec 03 '12 at 15:27
  • 2
    @Cerbrus: No... JSON != JavaScript. The JSON syntax is similar to *(and based on)* the literal notations of JavaScript, but that's where the similarity ends. They're two very different things. – I Hate Lazy Dec 03 '12 at 15:39
  • @user1689607: So, it's more like: "JavaScript-like Object Notation"? – Cerbrus Dec 03 '12 at 15:40
  • 1
    @Cerbrus: It's more like *"Language Independent Data Structure Notation"*. If it's JSON, it's ultimately Unicode textual data. Because of the similarity to JavaScript's literal notations, you can use `eval()` in most cases to process a string of JSON data into a JavaScript program, but it's ultimately a platform independent data interchange format... sort of like XML. – I Hate Lazy Dec 03 '12 at 15:43
  • JSON is valid Javascript, but valid Javascript is not automatically valid JSON. This Javascript here happens to not be valid JSON. – deceze Dec 03 '12 at 15:46
  • 2
    possible duplicate of [Self-references in object literal declarations](http://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations) – Bergi Jan 10 '14 at 16:11

2 Answers2

10

No. The best you can do is:

var dict = {
    'entrance' : {
        'rate1' : 5,
        'rate2' : 10,
        'rate3' : 20,
    }
};
dict.movies = {
    'theDarkKnight' : {
        '00:00' : dict.entrance.rate1,
        '18:00' : dict.entrance.rate2,
        '21:00' : dict.entrance.rate3
    },
    ...
};
deceze
  • 510,633
  • 85
  • 743
  • 889
3

You could use mustache and define your json as a "mustache template", then run mustache to render the template. Take into account you would need to run (n) times if you have nested dependencies. In this case you have 3 dependencies ABC --> AB --> A.

var mustache = require('mustache');

var obj = {
  A : 'A',
  AB : '{{A}}' + 'B',
  ABC : '{{AB}}' + 'C'
}

function render(stringTemplate){
  while(thereAreStillMustacheTags(stringTemplate)){
    stringTemplate = mustache.render(stringTemplate, JSON.parse(stringTemplate));
  }
  return stringTemplate;
}

function thereAreStillMustacheTags(stringTemplate){
  if(stringTemplate.indexOf('{{')!=-1)
    return true;
  return false;
}

console.log(render(JSON.stringify(obj)));

And the output is:

{"A":"A","AB":"AB","ABC":"ABC"}
cSn
  • 2,796
  • 3
  • 23
  • 29