是否有一种简洁的es6功能方式按类型对项目进行分组,因为它是不可变的?
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"}
]
预期
[
{"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"}
]
答案 0 :(得分:9)
您可以使用Array#reduce
按内容[testing]
This is testing
I want to remove content
[dummy]
some text here as well
this is a dummy file
属性对列表进行分组:
type