admin管理员组文章数量:1180536
Is there a terse es6 functional way to group items by type in that it's immutable?
accounts.json
[
{"account": {"name": "Bob's credit", "type": "credit", "id": "1"}},
{"account": {"name": "savings", "type": "savings", "id": "2"}},
{"account": {"name": "vacation savings", "type": "savings", "id": "3"}},
{"account": {"name": "son's savings", "type": "savings", "id": "4"},
{"account": {"name": "wife's credit card", "type": "savings", "id": "5"}
]
Expected
[
{"savings": [
{"account": {"name": "savings", "type": "savings", "id": "2"}},
{"account": {"name": "vacation savings", "type": "savings", "id": "3"}},
{"account": {"name": "son's savings", "type": "savings", "id": "4"}
]},
{"checking": [
{"account": {"name": "wife's credit card", "type": "savings", "id": "5"}
]
Is there a terse es6 functional way to group items by type in that it's immutable?
accounts.json
[
{"account": {"name": "Bob's credit", "type": "credit", "id": "1"}},
{"account": {"name": "savings", "type": "savings", "id": "2"}},
{"account": {"name": "vacation savings", "type": "savings", "id": "3"}},
{"account": {"name": "son's savings", "type": "savings", "id": "4"},
{"account": {"name": "wife's credit card", "type": "savings", "id": "5"}
]
Expected
[
{"savings": [
{"account": {"name": "savings", "type": "savings", "id": "2"}},
{"account": {"name": "vacation savings", "type": "savings", "id": "3"}},
{"account": {"name": "son's savings", "type": "savings", "id": "4"}
]},
{"checking": [
{"account": {"name": "wife's credit card", "type": "savings", "id": "5"}
]
Share
Improve this question
edited Mar 30, 2018 at 13:30
chrisl-921fb74d
asked Mar 30, 2018 at 13:28
chrisl-921fb74dchrisl-921fb74d
23.1k28 gold badges87 silver badges115 bronze badges
2
- Please share your attempt. – gurvinder372 Commented Mar 30, 2018 at 13:30
- there is no built-in function in es6 to achieve that, you need to filter the array by yourself or using third-party library like lodash. – anasceym Commented Mar 30, 2018 at 13:31
2 Answers
Reset to default 33You can use Array#reduce
to group your list by its elements inner type
property :
const data = [
{"account": {"name": "Bob's credit", "type": "credit", "id": "1"}},
{"account": {"name": "savings", "type": "savings", "id": "2"}},
{"account": {"name": "vacation savings", "type": "savings", "id": "3"}},
{"account": {"name": "son's savings", "type": "savings", "id": "4"}},
{"account": {"name": "wife's credit card", "type": "savings", "id": "5"}}
];
const res = data.reduce((acc, curr) => {
if(!acc[curr.account.type]) acc[curr.account.type] = []; //If this type wasn't previously stored
acc[curr.account.type].push(curr);
return acc;
},{});
console.log(res);
You can now use the latest release function Object.groupBy(array, callbackFun):
const result = Object.groupBy(data, ({type}) => type);
the code is more conscise and readeable.
本文标签: javascriptES6 group array objects by field typeStack Overflow
版权声明:本文标题:javascript - ES6 group array objects by field type - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738199855a2068344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论