I have such a JSON in JavaScript (Express.js/Mongoose):
// create a document
var demo = new DemoSchema({
hat: { money: "500" },
functions: {
func1: "this.hat.money - (0.02*this.hat.money)"
}
});
Now I want to use this string as objects. Can it be achieved?
e.g.
DemoSchema.virtual('hat.newValue').get(function() {
return this.functions.func1;
});
console.log('%s is 2% less', demo.hat.newValue);
// above prints: this.hat.money - (0.02*this.hat.money is 2% less)
More Background Info about why to do it this way: https://groups.google.com/forum/#!topic/mongoose-users/lzyjg0b8Vn0
Pointers: String to object in JS
Updated Code: http://jsfiddle.net/nottinhill/uktLr2g5/