【问题标题】:Create nested object from array of objects in JavaScript [closed]从 JavaScript 中的对象数组创建嵌套对象 [关闭]
【发布时间】:2019-09-23 19:11:06
【问题描述】:

我有一组对象数据:

[
  {"company": "Google", "country": "USA", "employee": "John"},
  {"company": "Amazon", "country": "UK", "employee": "Arya"},
  {"company": "Google", "country": "KSA", "employee": "Cersi"},
  {"company": "Amazon", "country": "USA", "employee": "Tyrion"},
  {"company": "Amazon", "country": "USA", "employee": "Daenarys"},
  {"company": "Google", "country": "KSA", "employee": "Dothrokhi"}
]

如何创建如下所示的嵌套对象?

{
  "Amazon": {
    "UK": {"Arya": null}, 
    "USA": {"Tyrion": null, "Daenarys": null}
  },
  "Google": {
    "KSA": {"Cersi": null, "Dothrokhi": null},
    "USA": {"John": null}
  }
}

【问题讨论】:

    标签: javascript arrays json object ecmascript-6


    【解决方案1】:

    您可以使用reduce 循环遍历数组并将其汇总为一个对象。

    let arr = [{"company":"Google","country":"USA","employee":"John"},{"company":"Amazon","country":"UK","employee":"Arya"},{"company":"Google","country":"KSA","employee":"Cersi"},{"company":"Amazon","country":"USA","employee":"Tyrion"},{"company":"Amazon","country":"USA","employee":"Daenarys"},{"company":"Google","country":"KSA","employee":"Dothrokhi"}]
    
    let result = arr.reduce((c, v) => {
      c[v.company] = c[v.company] || {};                         //Init if company property does not exist
      c[v.company][v.country] = c[v.company][v.country] || {};   //Init if country property does not exist
      c[v.company][v.country][v.employee] = null;                //Add employee property with null value
      return c;
    }, {});
    
    console.log(result);

    【讨论】:

      【解决方案2】:

      使用reduce:

      const data = [{"company":"Google","country":"USA","employee":"John"},{"company":"Amazon","country":"UK","employee":"Arya"},{"company":"Google","country":"KSA","employee":"Cersi"},{"company":"Amazon","country":"USA","employee":"Tyrion"},{"company":"Amazon","country":"USA","employee":"Daenarys"},{"company":"Google","country":"KSA","employee":"Dothrokhi"}];
      const res = data.reduce((acc, { company, country, employee }) => {
        acc[company] = acc[company] || {};
        acc[company][country] = acc[company][country] || {};
        acc[company][country][employee] = null;
        return acc;
      }, {});
      console.log(res);
      .as-console-wrapper { max-height: 100% !important; top: auto; }

      【讨论】:

        【解决方案3】:

        试试lodash:

        // Function to convert an array of an object into an object of null with a key
        function convert(array, key, value = null) {
          return Object.fromEntries(array.map(item => [item[key], value]));
        }
        
        // Sample data
        const data = [
          {"company": "Google", "country": "USA", "employee": "John"},
          {"company": "Amazon", "country": "UK", "employee": "Arya"},
          {"company": "Google", "country": "KSA", "employee": "Cersi"},
          {"company": "Amazon", "country": "USA", "employee": "Tyrion"},
          {"company": "Amazon", "country": "USA", "employee": "Daenarys"},
          {"company": "Google", "country": "KSA", "employee": "Dothrokhi"}
        ];
        
        // Using "groupBy" - Group by companies
        const companies = _.groupBy(data, obj => obj.company);
        
        // Using "mapValues" - Group each company's data by country
        const byCountry = _.mapValues(companies, company => _.groupBy(company, obj => obj.country));
        
        // Convert the array into employees object, or use _.keyBy if you want to keep the original data
        const result = _.mapValues(byCountry, company => _.mapValues(company, country => convert(country, 'employee')));
        
        // Result
        console.log(result);
        <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>

        【讨论】:

          猜你喜欢
          • 2019-10-06
          • 1970-01-01
          • 2021-06-07
          • 2015-10-18
          • 2021-07-12
          • 1970-01-01
          • 2020-12-21
          • 1970-01-01
          相关资源
          最近更新 更多