从 JavaScript 中的对象数组创建嵌套对象 [关闭]
Posted
技术标签:
【中文标题】从 JavaScript 中的对象数组创建嵌套对象 [关闭]【英文标题】:Create nested object from array of objects in JavaScript [closed] 【发布时间】: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
【问题讨论】:
【参考方案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>
【讨论】:
以上是关于从 JavaScript 中的对象数组创建嵌套对象 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
如何将 GraphQL 中的对象元素推送到 javascript 对象中的嵌套数组中?