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
Add a comment  | 

2 Answers 2

Reset to default 33

You 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