skip to Main Content

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' },
    ],
  }
]

3

Answers


  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()

    Login or Signup to reply.
  2. 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' } ]
    }
    
    Login or Signup to reply.
  3. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search