【问题标题】:Getting dynamic values from an array of objects and children by string [duplicate]通过字符串从对象和子对象数组中获取动态值[重复]
【发布时间】:2020-05-21 12:45:01
【问题描述】:

我想知道是否有一种方法可以使用字符串动态获取子对象:

例如:“industry.description”

var fields = [{
    id: "1",
    industry: {
      description: "Test Industry1"
    }
  },
  {
    id: "2",
    industry: {
      description: "Test Industry2"
    }
  }
  ];

  var field1 = "id";
  var field2 = "industry.description";

  var val1 = fields[0][field1];
  var val2 = fields[0][field2]; // How can I get 'Test Industry1' here?

  console.log(val1);
  console.log(val2);

【问题讨论】:

    标签: javascript arrays object


    【解决方案1】:

    要么做:

    fields[0]["industry"]["description"];
    

    或将其分成多个字段(例如[field2][field3],其中field2 = "industry" 和field3 = "description")。

    或者,使用reduce 处理您当前的字符串:

    var val2 = field2.split(".").reduce((o, k) => o[k], fields[0]);
    

    【讨论】:

      【解决方案2】:

      "industry.description" 键不起作用。 您需要先获取"industry" 键下的对象,然后从中获取"description"。 像这样:

      fields[0]["industry"]["description"] // "Test Industry1"
      

      【讨论】:

        【解决方案3】:

        允许通过动态提供的路径从对象中提取嵌套值的简单解决方案是:

        function getValue(object, path) {
        
          // Break input path into parts separated by '.' via split
          const parts = path.split(".");
        
          // Iterate the parts of the path, and reduce that parts array to a single
          // value that we incrementally search for by current key, from a prior
          // nested "value"
          return parts.reduce((value, key) => value ? value[key] : value, object)  
        }
        
        console.log(getValue(fields[0], "industry.description")); // Test Industry1
        

        这样做的好处是它很简单,并且不需要将另一个库添加到您的项目中。以下是它在您的代码上下文中的工作方式:

        function getValue(object, path) {
          
          const parts = path.split(".");
          
          return parts.reduce((value, key) => value ? value[key] : value, object)  
        }
        
        var fields = [{
            id: "1",
            industry: {
              description: "Test Industry1"
            }
          },
          {
            id: "2",
            industry: {
              description: "Test Industry2"
            }
          }
        ];
        
        var field1 = "id";
        var field2 = "industry.description";
        
        console.log(getValue(fields[0], field1))
        console.log(getValue(fields[0], field2))

        【讨论】:

          【解决方案4】:
          var fields = [{
              id: "1",
              industry: {
                description: "Test Industry1"
              }
            },
            {
              id: "2",
              industry: {
                description: "Test Industry2"
              }
            }
            ];
          
            var field1 = "id";
            var field2 = "industry";
            var subField2 = "description"
          
            var val1 = fields[0][field1];
            var val2 = fields[0][field2][subField2]; // How can I get 'Test Industry1' here?
          
            console.log(val1);
            console.log(val2);
          

          【讨论】:

            猜你喜欢
            • 2013-09-23
            • 2012-12-07
            • 1970-01-01
            • 2012-01-27
            • 2021-12-15
            • 1970-01-01
            • 2019-04-30
            相关资源
            最近更新 更多