按字段类型划分ES6组数组对象

时间:2018-03-30 13:28:46

标签: javascript collections ecmascript-6

是否有一种简洁的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"}
]

1 个答案:

答案 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