Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

100
Vistas
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 :

        [
         {
           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

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

you can use reduce to group by id and then the values of the resultant using Object.values
EDIT
??= is the Logical nullish assignment. The right hand side will be assigned whenever the left hand side is null or undefined.

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',         }        ]
        
const res = Object.values(items.reduce((acc,curr)=> {
  acc[curr.id]??=[]  //similar to acc[curr.id] = acc[curr.id] || [] in this case
  acc[curr.id].push(curr)
  return acc
},{}))

console.log(res)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda