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

5

我有一组对象数据:

[
  {"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个回答

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


9

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接