JSON
JavaScript Object Push method
Dec 12th
here is object.push or json.setNode method i implemented for my needs. the name of the function is not ‘push’ because push method used in Array which is a subset of Object.
Sample:
var test = { };
test.setNode("path.to.your.variable", "my value");
// result:
// alert(test.path.to.your.variable);
//
// will return 'my value'
Code:
Object.prototype.setNode = function(_key, _value) {
var module = this;
var spaceArray = _key ? _key.split('.') : [];
var subSpace = null;
var ndx = null;
for (ndx = 0; ndx < spaceArray.length - 1 && (subSpace = spaceArray[ndx]); ++ndx) {
if (subSpace) {
if (!module[subSpace]) {
module[subSpace] = {};
}
module = module[subSpace];
}
}
subSpace = spaceArray[ndx];
module[subSpace] = _value;
return this;
};


LinkedIn
Twitter
Skype