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

216
Views
Insertar filas a través de un bucle con JavaScript

tengo esta consulta:

 SELECT kailiv, count(*) as PALETTES from FGE50NEUV1.gesupe where etasup='30' and cumcol>0 and kailiv>=1 and kailiv<=66 group by kailiv order by kailiv

Esto devuelve cosas como:

 [ { KAILIV: 1, PALETTES: 15 }, { KAILIV: 2, PALETTES: 7 }, { KAILIV: 3, PALETTES: 2 }, { KAILIV: 6, PALETTES: 4 }, { KAILIV: 7, PALETTES: 1 }, { KAILIV: 13, PALETTES: 10 }, { KAILIV: 15, PALETTES: 3 }, { KAILIV: 20, PALETTES: 8 }, { KAILIV: 26, PALETTES: 2 }, { KAILIV: 27, PALETTES: 1 }, { KAILIV: 29, PALETTES: 10 }, { KAILIV: 30, PALETTES: 10 }, { KAILIV: 31, PALETTES: 4 }, { KAILIV: 32, PALETTES: 10 }, { KAILIV: 62, PALETTES: 7 }, { KAILIV: 63, PALETTES: 6 }, { KAILIV: 64, PALETTES: 4 } ]

Y me gustaría tener una fila para cada "kailiv" entre 1 y 66, con un 0 si está vacío

¿Es más fácil hacerlo después de obtener los datos a través de JS o directamente en SQL? ¿Cómo puedo hacerlo?

EDITAR: cuando intento esto después de la consulta:

 for(let i=1; i<=66; i++) { let quai = travees[i].KAILIV if (quai != i) { travees.slice(i-1,0,{KAILIV : i, PALETTES: 0}) } }

Tengo un error 'No se pueden leer las propiedades de undefined (leyendo 'KAILIV')

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Es bastante fácil de hacer en javascript, la idea general es convertir su matriz en una búsqueda codificada en KAILIV y luego simplemente hacer un bucle de 1 a 66 tirando del valor o 0

 const input = [ { KAILIV: 1, PALETTES: 15 }, { KAILIV: 2, PALETTES: 7 }, { KAILIV: 3, PALETTES: 2 }, { KAILIV: 6, PALETTES: 4 }, { KAILIV: 7, PALETTES: 1 }, { KAILIV: 13, PALETTES: 10 }, { KAILIV: 15, PALETTES: 3 }, { KAILIV: 20, PALETTES: 8 }, { KAILIV: 26, PALETTES: 2 }, { KAILIV: 27, PALETTES: 1 }, { KAILIV: 29, PALETTES: 10 }, { KAILIV: 30, PALETTES: 10 }, { KAILIV: 31, PALETTES: 4 }, { KAILIV: 32, PALETTES: 10 }, { KAILIV: 62, PALETTES: 7 }, { KAILIV: 63, PALETTES: 6 }, { KAILIV: 64, PALETTES: 4 } ] // make a map of the data const indexed = Object.fromEntries(input.map(x => [x.KAILIV,x.PALETTES])); // function to generate a range of numbers (1-66 in our case) function range(size, startAt = 0) { return [...Array(size).keys()].map(i => i + startAt); } // generate a new set of data const newData = range(66,1).map(k=> ({ KAILIV:k, PALETTES: indexed[k] ?? 0 })) console.log(newData);

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!