0

I have the following data structure in its simplest form:

items: [
   { id: 1, account: 'acct_101' },
   { id: 2, account: 'acct_52' },
   { id: 3, account: 'acct_33' },
   { id: 4, account: 'acct_101' },
   { id: 5, account: 'acct_101' },
]

I would like to separate the items into groups based on their account data. The data is dynamic; I don't know in advance what the account numbers may be.

I can loop through the data:

items.map((item) => (
   console.log("item account: ", item.account)
))

Yet unsure how to match if an item.account already exists and if so add to an existing data object, if not create a new object group.

The desired output could like like this:

item_groups:  [
  { 
    acct_101: [
      { id: 1, account: 'acct_101' },
      { id: 4, account: 'acct_101' },
      { id: 5, account: 'acct_101' },
    ],
    acct_52: [
      { id: 2, account: 'acct_52' },
    ],
    acct_33: [
     { id: 2, account: 'acct_33' },
    ],
  }
]
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Jake Anderson
  • 309
  • 1
  • 4
  • 18
  • Does this answer your question? [How can I group an array of objects by key?](https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key) In short, lodash's `.groupBy`, or loop through either with for..of or `.reduce` and aggregate into a Map or Object. – Digital Deception Feb 03 '23 at 04:56

3 Answers3

1

Try like below:

const items = [
   { id: 1, account: 'acct_101' },
   { id: 2, account: 'acct_52' },
   { id: 3, account: 'acct_33' },
   { id: 4, account: 'acct_101' },
   { id: 5, account: 'acct_101' },
]

const output = items.reduce((prev, curr) => {
  if(prev[curr.account]) {
    prev[curr.account].push(curr)
  } else {
    prev[curr.account] = [curr]
  }
  return prev
}, {})

console.log([output])

More shorter form:

const items = [
   { id: 1, account: 'acct_101' },
   { id: 2, account: 'acct_52' },
   { id: 3, account: 'acct_33' },
   { id: 4, account: 'acct_101' },
   { id: 5, account: 'acct_101' },
]

const output = items.reduce((prev, curr) => {
  prev[curr.account] = (prev[curr.account] ?? []).concat(curr)
  return prev
}, {})

console.log([output])

Using Array.prototype.reduce()

Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
1

For your desired output I think you need to use reduce higher order function.

So your code will be :

const itemGroups = items.reduce((acc, item) => {
  const account = item.account;
  if (acc[account]) {
    acc[account].push(item);
  } else {
    acc[account] = [item];
  }
  return acc;
}, {});

console.log(itemGroups);

Output :

{
  acct_101: [
    { id: 1, account: 'acct_101' },
    { id: 4, account: 'acct_101' },
    { id: 5, account: 'acct_101' }
  ],
  acct_52: [ { id: 2, account: 'acct_52' } ],
  acct_33: [ { id: 3, account: 'acct_33' } ]
}
NIKUNJ PATEL
  • 2,034
  • 1
  • 7
  • 22
0
const arr = [{
    id: 1,
    account: 'acct_101'
  },
  {
    id: 2,
    account: 'acct_52'
  },
  {
    id: 3,
    account: 'acct_33'
  },
  {
    id: 4,
    account: 'acct_101'
  },
  {
    id: 5,
    account: 'acct_101'
  },
];
const retObj = {};
for (let i = 0; i < arr.length; i++) {

  retObj[arr[i]["account"]] = arr.filter(item => item.account == arr[i]["account"]);
}
console.log(retObj)
Akhil
  • 443
  • 1
  • 9
  • 29