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

149
Vistas
Manipulating Nested JSON Arrays

i have bellow json array

var rowdataarray = [
                     {
                      "products": "Cars",
                      "solddate" : "2022-01-01",
                      "noofitems" : "7"
                     },
                     {
                      "products": "books",
                      "solddate" : "2022-01-01",
                      "noofitems" : "10"
                     },
                     {
                      "products": "Cars",
                      "solddate" : "2022-01-02",
                      "noofitems" : "2"
                     },
                     {
                      "products": "Table",
                      "solddate" : "2022-01-02",
                      "noofitems" : "5"
                     }
                   ];

and then have category array

const category = ["Cars","books","Table"]

based on these two array trying to create new array which will give me each day for each product how many items sold. above category must follow when creating data array. if the product is not sold on that day then it should insert as 0.

array expecting

var finalarray = [{
        date: '2022-01-01',
        data: [7,10,0]
    }, {
        date: '2022-01-02',
        data: [2,0,5]
    }]

bellow code written not able to contiunue forward

 $.when(
 $.each(category , function (key, val) {

        $.each(rowdataarray , function (key, x) {
    
                if(x.products == val){
                    finalarray .push({
                        "date" : x.solddate,
                        "data" : [x.noofitems]
                    })
                }
        })
 })
 ).then(function () {
    console.log(test)     
   });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

  1. Collect the data by date and associate each count by product
  2. Map over the entries and query each for your category

var rowdataarray = [{"products":"Cars","solddate":"2022-01-01","noofitems":"7"},{"products":"books","solddate":"2022-01-01","noofitems":"10"},{"products":"Cars","solddate":"2022-01-02","noofitems":"2"},{"products":"Table","solddate":"2022-01-02","noofitems":"5"}]
const category = ["Cars","books","Table"]

const byDate = rowdataarray.reduce((m, {
  solddate: d, 
  products: p, 
  noofitems: c
}) => ({
  ...m,
  [ d ]: { // the date is the top-level key
    ...m[d],
    [ p ]: (m[d]?.[p] ?? 0) + parseFloat(c) // add the count to any existing product count
  }
}), {})
console.log("byDate", byDate)

const finalarray = Object.entries(byDate).map(([ date, counts ]) => ({
  date,
  data: category.map(cat => counts[cat] ?? 0)
}))

console.info("finalarray", finalarray)
.as-console-wrapper { max-height: 100% !important; }

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