Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda