Too much recursion

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 .

+3
source share
4 answers

I am not 100% familiar with how to do something in Javascript, but essentially you need 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"

, , explorable. , explorable.

, .

+4

if(objectSize(_object) > 0) { if(objectSize(_object[property]) > 0) {?

+1

You do not use a .hasOwnProperty()for loop inside updateObject. Could this find some kind of built-in property that is essentially "infinite" in depth?

+1
source

Here is an idea:

function updateObject(_value, _property, _object) {
    function u(v, p, o) {
      if (o === _object) return;
      if (o.hasOwnProperty(p))
        o[p] = v;
      else {
        for (var prop in o)
          if (o.hasOwnProperty(prop))
            u(v, p, o[prop]);
      }
    }
    u(_value, _property, _object);
    return _object
};

This adds a test to make sure that you do not go back to the original object.

0
source

Source: https://habr.com/ru/post/1747674/


All Articles