Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
JS creando una nueva matriz de objetos usando otro objeto

Tengo debajo de la matriz de objetos

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

Quiero agrupar cada categoría y luego por tipo y contar de la siguiente manera:

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

hasta ahora he escrito la siguiente lógica pero no me da el resultado esperado, solo está agregando los últimos valores de cada categoría

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

0

Podrías intentar algo como esto:

 $.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 })

Manifestación

 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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!