【问题标题】:How do you access an object within an object from an argument in Javascript? [duplicate]如何从 Javascript 中的参数访问对象中的对象? [复制]
【发布时间】:2013-02-02 23:28:46
【问题描述】:

可能重复:
Accessing nested JavaScript objects with string key

我有这个功能

function _get(name) {
return plugin._optionsObj[name] !== undefined ? 
    plugin._optionsObj[name] : plugin._defaults[name];
}

我希望能够在我的 _defaults 对象中包含对象,但是我不知道如何检索它们,只使用一组方括号。

plugin._defaults = {
    val1: 1,
    val2: 2,
    obj1: {
        someVal: 3
    }
}

是否可以从我上面的函数中访问“someVal”?我尝试为参数传递'obj1.someVal',但它没有用。想法?

编辑:我找到了解决方案,并将其发布在下面作为答案。我编写了一个非常好的小函数来通过字符串遍历嵌套值,并且我不必更改我的函数来实现它。我希望这对处于类似情况的任何人有所帮助。

【问题讨论】:

  • 返回 plugin._optionsObj[name] !== undefined ? plugin._optionsObj[name] : plugin._defaults[obj1][someVal];不工作?
  • 看这个要点:gist.github.com/3208381#file__.deep.js...如果你使用下划线,你只需将路径(例如'obj1.someVal')作为字符串传递,它会遍历对象图以找到嵌套的价值。
  • 我正在寻找是否有办法在不改变我的功能或实现的情况下解决这个问题。
  • @VladMagdalin 你能举个例子吗?我不确定您所说的使用下划线是什么意思。
  • 不改变你的 _get() 函数,不,不可能解决这个问题,除非你可以改变你的数据结构。 Underscore 是一个库(类似于 jQuery),可帮助您处理 JavaScript 对象。我发布的 _.deep() mixin 是 Underscore 的插件。

标签: javascript oop object syntax


【解决方案1】:

我怀疑你不会总是有一个一级嵌套对象可以访问,所以更简洁的方法是使用一个基于字符串路径遍历对象的函数。 Here 被编码为 Underscore 的 mixin。然后你可以像这样使用它:

_.deep(plugin._defaults, 'obj1.someVal');

This thread 也有一些非下划线选项。

【讨论】:

  • 我最终编写了自己的函数,但谢谢;你的回答很有用。
  • 太棒了,很高兴你明白了。不要忘记在此处发布您的解决方案。
【解决方案2】:

传递多个参数,并遍历 arguments 对象。

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;
}

var opt = _get("obj1", "someVal")

【讨论】:

    【解决方案3】:

    我找到了解决这个问题的方法,至少可以解决我自己的问题,我想分享它,以防它可以帮助其他人解决这个问题。我最大的困难是我不知道嵌套值的深度,所以我想找到一个适用于深度嵌套对象并且不需要重新设计任何东西的解决方案。

    /* 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];
        }
    }
    

    【讨论】:

      【解决方案4】:

      要检索 _defaults 对象内的对象,您需要改进 _get 函数。

      例如,您可以将字符串数组(每个字符串代表一个属性名称)传递给 _get 以允许访问深度嵌套的对象。

      【讨论】:

        猜你喜欢
        • 2023-03-15
        • 2023-03-13
        • 1970-01-01
        • 2018-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-28
        • 1970-01-01
        相关资源
        最近更新 更多