Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

too much recursion

In this recursive function, I want to replace a value inside a (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 running this, firefox throws the exception "too much recursion" on the line else if(objectSize(_object) > 0) { .

Edit: if I set

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 only searches one level. If I had an nested object inside a nested object, it wouldn't work.

Any more ideas?

Edit: This problem occurs in Firefox 3.6. It works in Chrome.

like image 368
Dänu Avatar asked May 02 '26 16:05

Dänu


2 Answers

I'm not 100% familiar with how to do things in Javascript, but essentially you want something like this:

var testobj = {
    'user': {
        'name': 'Mario',
        'password': 'itseme',
        'bleh': {
            'password': 'something'
        }
    }
};

function updateObject(_value, _property, _object) {
    for(var property in _object) {
        if(property == _property) {
            _object[property] = _value;
        }
        else if(explorable(_object[property])) {
            updateObject(_value, _property, _object[property]);
        }
    }

    return _object
};

function explorable(_object) {
    return typeof(_object) != "string";
};

updateObject('emesti', 'password', testobj);
document.writeln(testobj.user.password);      // "emesti"
document.writeln(testobj.user.bleh.password); // "emesti"

Currently, anything that isn't a string is explorable. This may or may not work for all cases, so you may want to use a better definition of what is explorable.

Also note that the recursion right now updates ALL matching properties.

like image 102
polygenelubricants Avatar answered May 05 '26 06:05

polygenelubricants


shouldn't if(objectSize(_object) > 0) { be if(objectSize(_object[property]) > 0) {?

like image 33
x1a4 Avatar answered May 05 '26 06:05

x1a4