太多的递归
在此递归函数中,我想替换(嵌套)对象内的值。
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;
};
运行此命令后,Firefox 在 else if(objectSize(_object) > 0) {
行上抛出异常“too much recursion”。
编辑: 如果我设置
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
};
它可以工作,但它只搜索一层。如果我在嵌套对象内有一个嵌套对象,它将无法工作。
还有更多想法吗?
编辑: 此问题出现在 Firefox 3.6 中。它适用于 Chrome。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我并不是 100% 熟悉如何在 Javascript 中做事,但本质上你想要这样的东西:
目前,任何不是字符串的东西都是
可探索的
。这可能适用于所有情况,也可能不适用于所有情况,因此您可能需要对可探索
使用更好的定义。另请注意,递归现在会更新所有匹配属性。
I'm not 100% familiar with how to do things in Javascript, but essentially you want something like this:
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 isexplorable
.Also note that the recursion right now updates ALL matching properties.
if(objectSize(_object) > 0) {
不应该是if(objectSize(_object[property]) > 0) {
吗?shouldn't
if(objectSize(_object) > 0) {
beif(objectSize(_object[property]) > 0) {
?您没有在
updateObject
内的 for 循环中使用.hasOwnProperty()
。它是否会发现某种本质上“无限”深度的内置属性?You're not using
.hasOwnProperty()
in the for loop insideupdateObject
. Could it be finding some kind of in-built property which is essentially "infinite" in depth?这是一个想法:
添加一个测试以确保您不会从原始对象重新开始。
Here's an idea:
That adds a test to make sure you don't start over back at the original object.