如何从Javascript中的参数访问对象中的对象?
可能重复:如何从Javascript中的参数访问对象中的对象?,javascript,oop,object,syntax,Javascript,Oop,Object,Syntax,可能重复: 我有这个功能 function _get(name) { return plugin._optionsObj[name] !== undefined ? plugin._optionsObj[name] : plugin._defaults[name]; } 我希望能够在我的_defaults对象中包含对象,但是我不知道如何检索它们,只使用一组方括号 i、 e 是否可以从我上面的函数访问“someVal”?我试着传递'obj1.someVal'作为参数,但没有效果。想法
我有这个功能
function _get(name) {
return plugin._optionsObj[name] !== undefined ?
plugin._optionsObj[name] : plugin._defaults[name];
}
我希望能够在我的_defaults对象中包含对象,但是我不知道如何检索它们,只使用一组方括号
i、 e
是否可以从我上面的函数访问“someVal”?我试着传递'obj1.someVal'作为参数,但没有效果。想法
编辑:我找到了一个解决方案,并将其作为答案发布在下面。我已经编写了一个非常好的小函数,用一个字符串遍历嵌套的值,我不需要对函数做太多的修改就可以实现它。我希望这对类似情况下的任何人都有帮助。要检索默认对象中的对象,您需要改进\u get功能
例如,您可以将一个字符串数组(每个字符串表示一个属性名)传递给\u get,以允许访问深度嵌套对象。我怀疑您并不总是可以访问一个一级嵌套对象,因此更干净的方法是使用基于字符串路径遍历对象的函数。”s一个被编码为下划线的混音。然后,您可以像这样使用它:
_.deep(plugin._defaults, 'obj1.someVal');
还有一些非下划线选项。传递多个参数,并在
参数
对象上迭代
function _get(/* name1, name2, namen */) {
var item = plugin._optionsObj,
defItem = plugin._defaults;
for (var i = 0; i < arguments.length; i++) {
item = item[arguments[i]];
defItem = defItem[arguments[i]];
if (item == null || defItem == null)
break;
}
return item == null ? defItem : item;
}
我找到了一个解决这个问题的方法,至少一个适合我自己的方法,我想和大家分享一下,以防它能帮助其他人解决这个问题。我最大的困难是我不知道嵌套值的深度,所以我想找到一个解决方案,该解决方案适用于深度嵌套的对象,而无需重新设计任何内容
/* Retrieve the nested object value by using a string.
The string should be formatted by separating the properties with a period.
@param obj object to pass to the function
propertyStr string containing properties separated by periods
@return nested object value. Note: may also return an object */
function _nestedObjVal(obj, propertyStr) {
var properties = propertyStr.split('.');
if (properties.length > 1) {
var otherProperties = propertyStr.slice(properties[0].length+1); //separate the other properties
return _nestedObjVal(obj[properties[0]], otherProperties); //continue until there are no more periods in the string
} else {
return obj[propertyStr];
}
}
function _get(name) {
if (name.indexOf('.') !== -1) {
//name contains nested object
var userDefined = _nestedObjVal(plugin._optionsObj, name);
return userDefined !== undefined ? userDefined : _nestedObjVal(plugin._defaults, name);
} else {
return plugin._optionsObj[name] !== undefined ?
plugin._optionsObj[name] : plugin._defaults[name];
}
}
返回插件。_optionsObj[name]!==未定义?plugin._optionsObj[name]:plugin._默认值[obj1][someVal];不起作用?请看以下要点:。。。如果使用下划线,只需将路径(例如“obj1.someVal”)作为字符串传递,它将遍历对象图以查找嵌套值。我想看看是否有一种方法可以在不更改函数或其实现的情况下解决此问题。@VladMagdalin您能举个例子吗?我不知道你使用下划线是什么意思。如果不更改_get()函数,不可能解决这个问题,除非你可以更改数据的结构。是一个库(类似于jQuery),可以帮助您处理JavaScript对象。我发布的u.deep()mixin是一个下划线插件。我最终编写了自己的函数,但谢谢你;你的答案很有用。太棒了,很高兴你找到了答案。别忘了在这里发布你的解决方案。
var opt = _get("obj1", "someVal")
/* Retrieve the nested object value by using a string.
The string should be formatted by separating the properties with a period.
@param obj object to pass to the function
propertyStr string containing properties separated by periods
@return nested object value. Note: may also return an object */
function _nestedObjVal(obj, propertyStr) {
var properties = propertyStr.split('.');
if (properties.length > 1) {
var otherProperties = propertyStr.slice(properties[0].length+1); //separate the other properties
return _nestedObjVal(obj[properties[0]], otherProperties); //continue until there are no more periods in the string
} else {
return obj[propertyStr];
}
}
function _get(name) {
if (name.indexOf('.') !== -1) {
//name contains nested object
var userDefined = _nestedObjVal(plugin._optionsObj, name);
return userDefined !== undefined ? userDefined : _nestedObjVal(plugin._defaults, name);
} else {
return plugin._optionsObj[name] !== undefined ?
plugin._optionsObj[name] : plugin._defaults[name];
}
}