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

96
Vistas
JS creating new object array using another object

I have below object array

var testdata = [{
                 TYPE: 'type 1', Category: 'Booking', Count : 5
               },
               {
                 TYPE: 'type 2', Category: 'Booking', Count : 15
               },
               {
                 TYPE: 'type 1', Category: 'Planning', Count : 10
               },
               {
                 TYPE: 'type 3', Category: 'SALES', Count : 5
               }]

I want to group each category and then by type and count as below:

 var resultdata =
    {
     "Booking": {
       "type 1": 5,
       "type 2": 15
     },
    "Planning": {
       "type 1": 10
     },
    "SALES": {
       "type 3": 5
     },
    }

so far I have written bellow logic but it fails to give me expected result it is only adding the last values of each category

 $.each(testdata , function (key, value) {
            if(value.Category == "Booking"){
                bookingarray['Booking'] = {[value.TYPE]: value.Count}
            }            
         })
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could try something like this:

$.each(testdata, function(key, value) {
  if (!bookingarray[value.Category]) {
    bookingarray[value.Category] = {} // if example "Booking" does not exist in resultData, then create it
  }
  bookingarray[value.Category][value.TYPE] = value.Count
})

Demo

var testdata = [{
    TYPE: 'type 1',
    Category: 'Booking',
    Count: 5
  },
  {
    TYPE: 'type 2',
    Category: 'Booking',
    Count: 15
  },
  {
    TYPE: 'type 1',
    Category: 'Planning',
    Count: 10
  },
  {
    TYPE: 'type 3',
    Category: 'SALES',
    Count: 5
  }
]

var bookingarray = {};

$.each(testdata, function(key, value) {
  if (!bookingarray[value.Category]) {
    bookingarray[value.Category] = {}
  }
  bookingarray[value.Category][value.TYPE] = value.Count
})

console.log(bookingarray)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

const testdata = [
  {
    TYPE: 'type 1', Category: 'Booking', Count : 5
  }, {
    TYPE: 'type 2', Category: 'Booking', Count : 15
  }, {
    TYPE: 'type 1', Category: 'Planning', Count : 10
  }, {
    TYPE: 'type 3', Category: 'SALES', Count : 5
  }
];
    
let categories = new Set(testdata.map(t => t.Category));
let resultData = {};

categories.forEach(c => testdata.map(t => {
  if (t.Category === c) {
    if(resultData[c]) resultData [c][t.TYPE] = t.Count;
    else resultData = { ...resultData , [c]: { [t.TYPE]: t.Count } }; 
  }
}));

console.log(resultData);

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