从JavaScript中的对象数组创建嵌套对象

时间:2019-05-06 07:06:11

标签: javascript arrays json object ecmascript-6

我有对象数据数组:

[
  {"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}
  }
}

任何建议或代码示例都将非常有帮助。我正在尝试,但无法获得正确的结果。

3 个答案:

答案 0 :(得分:4)

您可以使用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);

答案 1 :(得分:4)

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

答案 2 :(得分:1)

您也可以使用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>