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?
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?
Share Improve this question edited Dec 13, 2018 at 9:16 Teemu 23.4k7 gold badges58 silver badges111 bronze badges asked Dec 3, 2012 at 15:17 Dominik HDominik H 3132 gold badges6 silver badges15 bronze badges 8- 3 Unfortunately, JSON doesn't support recursion/self-referencing. – Alvin Wong Commented Dec 3, 2012 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 Commented Dec 3, 2012 at 15:19
- 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 Commented Dec 3, 2012 at 15:39
-
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 Commented Dec 3, 2012 at 15:43 - 2 possible duplicate of Self-references in object literal declarations – Bergi Commented Jan 10, 2014 at 16:11
2 Answers
Reset to default 10No. 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
},
...
};
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"}