[英]Javascript: Convert list of objects to list of nested objects using groupBy
這是我正在使用的 JavaScript 對象的列表:
var filtered = [
{'faculty': 'Faculty of Sciences',
'degree': 'bachelor',
'majorName': 'Computer Science'},
{'faculty': 'Faculty of Sciences',
'degree': 'bachelor',
'majorName': 'Business Computing'},
{'faculty': 'Faculty of Sciences',
'degree': 'masters',
'majorName': 'Computer Science'},
{'faculty': 'Faculty of Sciences',
'degree': 'masters',
'majorName': 'Business Computing'},
{'faculty': 'School of Arts',
'degree': 'bachelor',
'majorName': 'Graphic Design'},
{'faculty': 'School of Arts',
'degree': 'bachelor',
'majorName': 'Media and Communication'},
{'faculty': 'School of Arts',
'degree': 'masters',
'majorName': 'Musicology'},
{'faculty': 'School of Arts',
'degree': 'masters',
'majorName': 'Media'}]
我想通過對教師和學位進行分組,將其轉換為嵌套對象列表。
請運行它以查看我當前的結果是什么:
var filtered = [{ 'faculty': 'Faculty of Sciences', 'degree': 'bachelor', 'majorName': 'Computer Science' }, { 'faculty': 'Faculty of Sciences', 'degree': 'bachelor', 'majorName': 'Business Computing' }, { 'faculty': 'Faculty of Sciences', 'degree': 'masters', 'majorName': 'Computer Science' }, { 'faculty': 'Faculty of Sciences', 'degree': 'masters', 'majorName': 'Business Computing' }, { 'faculty': 'School of Arts', 'degree': 'bachelor', 'majorName': 'Graphic Design' }, { 'faculty': 'School of Arts', 'degree': 'bachelor', 'majorName': 'Media and Communication' }, { 'faculty': 'School of Arts', 'degree': 'masters', 'majorName': 'Musicology' }, { 'faculty': 'School of Arts', 'degree': 'masters', 'majorName': 'Media' } ] var grouped = _.mapValues(_.groupBy(filtered, 'faculty'), flist => flist.map(filtered => _.omit(filtered, 'faculty'))); _.forEach(grouped, function(value, key) { grouped[key] = _.groupBy(grouped[key], function(item) { return item.degree; }); }); console.log(grouped);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
我怎樣才能省略留下的“度”屬性? 並將“majorName”屬性的值保留在屬於生成的學位屬性的列表中?
最終結果可能是這樣的:
{
"Faculty of Sciences": {
"bachelor": ["Computer Science", "Business Computing"],
"masters": ["Computer Science", "Business Computing"]
},
"School of Arts": {
"bachelor": ["Graphic Design", "Media and Communication"],
"masters": ["Musicology", "Media"]
}
}
謝謝:)
使用reduce()
、 解構和邏輯無效賦值 (??=)為您提供了一個簡潔的 vanilla 解決方案。
const filtered = [{ 'faculty': 'Faculty of Sciences', 'degree': 'bachelor', 'majorName': 'Computer Science' }, { 'faculty': 'Faculty of Sciences', 'degree': 'bachelor', 'majorName': 'Business Computing' }, { 'faculty': 'Faculty of Sciences', 'degree': 'masters', 'majorName': 'Computer Science' }, { 'faculty': 'Faculty of Sciences', 'degree': 'masters', 'majorName': 'Business Computing' }, { 'faculty': 'School of Arts', 'degree': 'bachelor', 'majorName': 'Graphic Design' }, { 'faculty': 'School of Arts', 'degree': 'bachelor', 'majorName': 'Media and Communication' }, { 'faculty': 'School of Arts', 'degree': 'masters', 'majorName': 'Musicology' }, { 'faculty': 'School of Arts', 'degree': 'masters', 'majorName': 'Media' }], groupedObjs = filtered.reduce((a, { faculty, degree, ...rest }) => (((a[faculty]??= { [degree]: [] })[degree]??= []).push({...rest }), a), {}), groupedStrings = filtered.reduce((a, { faculty, degree, majorName }) => (((a[faculty]??= { [degree]: [] })[degree]??= []).push(majorName), a), {}); console.log(groupedStrings); console.log(groupedObjs);
.as-console-wrapper { max-height: 100%;important: top; 0; }
或沒有邏輯無效分配...
const filtered = [{ 'faculty': 'Faculty of Sciences', 'degree': 'bachelor', 'majorName': 'Computer Science' }, { 'faculty': 'Faculty of Sciences', 'degree': 'bachelor', 'majorName': 'Business Computing' }, { 'faculty': 'Faculty of Sciences', 'degree': 'masters', 'majorName': 'Computer Science' }, { 'faculty': 'Faculty of Sciences', 'degree': 'masters', 'majorName': 'Business Computing' }, { 'faculty': 'School of Arts', 'degree': 'bachelor', 'majorName': 'Graphic Design' }, { 'faculty': 'School of Arts', 'degree': 'bachelor', 'majorName': 'Media and Communication' }, { 'faculty': 'School of Arts', 'degree': 'masters', 'majorName': 'Musicology' }, { 'faculty': 'School of Arts', 'degree': 'masters', 'majorName': 'Media' }], groupedObjs = filtered.reduce((a, { faculty, degree, ...rest }) => { a[faculty] = a[faculty] || { [degree]: [] }; (a[faculty][degree] = a[faculty][degree] || []).push({...rest }); return a }, {}), groupedStrings = filtered.reduce((a, { faculty, degree, majorName }) => { a[faculty] = a[faculty] || { [degree]: [] }; (a[faculty][degree] = a[faculty][degree] || []).push(majorName); return a }, {}); console.log(groupedStrings); console.log(groupedObjs);
.as-console-wrapper { max-height: 100%;important: top; 0; }
您可以將.map
中的每個 arrays 再次設置為僅 select majorName
字段。
這是一個例子
const filtered = [{ 'faculty': 'Faculty of Sciences', 'degree': 'bachelor', 'majorName': 'Computer Science' }, { 'faculty': 'Faculty of Sciences', 'degree': 'bachelor', 'majorName': 'Business Computing' }, { 'faculty': 'Faculty of Sciences', 'degree': 'masters', 'majorName': 'Computer Science' }, { 'faculty': 'Faculty of Sciences', 'degree': 'masters', 'majorName': 'Business Computing' }, { 'faculty': 'School of Arts', 'degree': 'bachelor', 'majorName': 'Graphic Design' }, { 'faculty': 'School of Arts', 'degree': 'bachelor', 'majorName': 'Media and Communication' }, { 'faculty': 'School of Arts', 'degree': 'masters', 'majorName': 'Musicology' }, { 'faculty': 'School of Arts', 'degree': 'masters', 'majorName': 'Media' }]; const grouped = _(filtered).groupBy('faculty').mapValues(l => _(l).map(o => _.omit(o, 'faculty')).groupBy("degree").mapValues(x => _(x).map(y => y.majorName)) ) console.log(grouped);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.