Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

separate json object into different index

I have following array which consist of json objects:

items = [
         {
           id: '1',
           name: 'Josh',
           transactionDate: '2012-08-10',
           creditAmount: '200',
           numberBank: '12345',
         },
         {
           id: '1',
           name: 'Josh',
           transactionDate: '2012-08-14',
           creditAmount: '159',
           numberBank: '12345',
         },
         {
           id: '1',
           name: 'Josh',
           transactionDate: '2012-08-15',
           creditAmount: '3421',
           numberBank: '12345',
         },
         {
           id: '2',
           name: 'George',
           transactionDate: '2012-09-15',
           creditAmount: '6000',
           numberBank: '13345',
         },
         {
           id: '2',
           name: 'George',
           transactionDate: '2012-09-16',
           creditAmount: '6565',
           numberBank: '13345',
         }
        ]

I want to separate the array index for each same id

as an example :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        [
         {
           id: '1',
           name: 'Josh',
           transactionDate: '2012-08-10',
           creditAmount: '200',
           numberBank: '12345',
         },
         {
           id: '1',
           name: 'Josh',
           transactionDate: '2012-08-14',
           creditAmount: '159',
           numberBank: '12345',
         },
         {
           id: '1',
           name: 'Josh',
           transactionDate: '2012-08-15',
           creditAmount: '3421',
           numberBank: '12345',
         }
        ],
        [
         {
           id: '2',
           name: 'George',
           transactionDate: '2012-09-15',
           creditAmount: '6000',
           numberBank: '13345',
         },
         {
           id: '2',
           name: 'George',
           transactionDate: '2012-09-16',
           creditAmount: '6565',
           numberBank: '13345',
         }
        ]

How to do like that ? thanks

>Solution :

you can use reduce to group by id and then the values of the resultant using Object.values

let items = [
         {
           id: '1',
           name: 'Josh',
           transactionDate: '2012-08-10',
           creditAmount: '200',
           numberBank: '12345',
         },
         {
           id: '1',
           name: 'Josh',
           transactionDate: '2012-08-14',
           creditAmount: '159',
           numberBank: '12345',
         },
         {
           id: '1',
           name: 'Josh',
           transactionDate: '2012-08-15',
           creditAmount: '3421',
           numberBank: '12345',
         },
         {
           id: '2',
           name: 'George',
           transactionDate: '2012-09-15',
           creditAmount: '6000',
           numberBank: '13345',
         },
         {
           id: '2',
           name: 'George',
           transactionDate: '2012-09-16',
           creditAmount: '6565',
           numberBank: '13345',
         }
        ]
        
let res = Object.values(items.reduce((acc,curr)=> {
    if(!acc[curr.id])acc[curr.id]=[]
  acc[curr.id].push(curr)
  return acc
},{}))

console.log(res)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading