In this recursive function, I want to replace the value inside an (nested) object.
var testobj = {
'user': {
'name': 'Mario',
'password': 'itseme'
}
};
updateObject('emesti', 'password', testobj)
function updateObject(_value, _property, _object) {
for(var property in _object) {
if(property == _property) {
_object[property] = _value;
}
else if(objectSize(_object) > 0) {
updateObject(_value, _property, _object[property]);
}
}
return _object
};
function objectSize(_object) {
var size = 0, key;
for (key in _object) {
if (_object.hasOwnProperty(key)) size++;
}
return size;
};
After starting firefox throws an exception "too much recursion" in the string else if(objectSize(_object) > 0) {.
Edit: if I installed
function updateObject(_value, _property, _object) {
for(var property in _object) {
if(property == _property) {
_object[property] = _value;
}
else if(_object[property].hasOwnProperty(_property)) {
updateObject(_value, _property, _object[property]);
}
}
return _object
};
It works, but it searches only one level. If I had a nested object inside a nested object, this would not work.
Any ideas?
Edit: This issue occurs in Firefox 3.6 . It works in Chrome .
source
share