【问题标题】:Get object with string identifier获取带有字符串标识符的对象
【发布时间】:2015-10-12 07:28:49
【问题描述】:

我需要帮助在 JS 中使用 String 获取对象的属性。

我有对象

elements = {
    element : {
            date: {
                 day: 'Monday'
            }
    }
}

我有输入是 "element.date.day" 的 JS 函数。和 基本上我需要做这样的事情:

function getObjectByStringIdentifier ( stringId ) {
    return elements[stringId];
}

这在 JS 中可行吗?

【问题讨论】:

    标签: javascript string object properties identifier


    【解决方案1】:

    你可以这样做

    var elements = {
      element: {
        date: {
          day: 'Monday'
        },
        example: {
          abc: 'hii'
        }
      }
    };
    
    function getObjectByStringIdentifier(stringId) {
      stringId = stringId.split('.');
      // split string using `.`
      var res = elements;
      // define res as object
      for (var i = 0; i < stringId.length; i++)
      // iterate over array
        res = res[stringId[i]]
        // update res as inner object value
      return res;
      // return result
    }
    
    console.log(getObjectByStringIdentifier("element.date.day"));
    console.log(getObjectByStringIdentifier("element.example.abc"));

    【讨论】:

      【解决方案2】:

      你可以这样做:

      var elements = {
          element : {
                  date: {
                       day: 'Monday'
                  }
          },
          cars : {
              racing : "Lamborghini",
              classic: "Rolls Royce"  
          }
      }
      
      
      function getObjectByStringIdentifier ( stringId ) {
          objects = stringId.split(".");
          element = elements; 
          for(i=0; i < objects.length; i++)
              element = element[objects[i]]; 
          return element;
      }
      
      alert(getObjectByStringIdentifier("cars.racing"));
      alert(getObjectByStringIdentifier("element.date.day"));
      alert(getObjectByStringIdentifier("cars.classic"));

      【讨论】:

        【解决方案3】:

        是的可以! :-)

        var elements = {
            element : {
                    date: {
                         day: 'Monday'
                    }
            }
        }
        
        function getObjectByStringIdentifier ( stringId ) {
            
            //return elements[stringId];
            //            ^      ^  
            //            |      |  
            //            |------|---------------------------------------------|  
            //                   |                                             |
            //        -----------|                                             | 
            //        |                                                        |
            //        °                                                        °
            return stringId.split('.').reduce(function(t,v){return t[v]; } , elements)
        }
        
        
        /** let's test it now ! **/
        
        var stringIdentifier = "element.date.day";
        var result = getObjectByStringIdentifier( stringIdentifier );
        
        
        document.getElementById('el').innerHTML = result;
        &lt;div id='el'&gt;&lt;/div&gt;

        【讨论】:

          【解决方案4】:

          现代浏览器支持 JSON.parse()。

          var arr_from_json = JSON.parse( json_string );
          

          在不支持的浏览器中,您可以包含 json2 库。

          alert(arr_from_json.elements);
          

          【讨论】:

          • 你试过你的建议了吗?这不符合 OP 的需要!
          猜你喜欢
          • 2016-03-19
          • 1970-01-01
          • 2021-11-07
          • 1970-01-01
          • 2021-03-12
          • 2021-05-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多