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

109
Vistas
how to push an array inside an array with nested nested loop

With the following code, I want to push an array inside the weeks array if i is <= to the value of each element in that array.

const weeks = [];
const daysInWeek = [];
numberOfDays = 35;

for (let i = 1; i <= numberOfDays; i ++) {
  if (i % 7 === 0){
    weeks.push(i)
  }
  // output [7, 14, 21, 28, 35]
  for (logic here...) {
    daysInWeek.push([?])
  }
}

I would like daysInWeek to look something like this :

[[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15,......], ...]

It is the first time I work with nested loops and I can't really grasp the idea and therefor I am unsure how to use the second loop.

Any help would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Create a new array inside of the if (i % 7 === 0){ statement. That array is going to be created for every week and is going to hold your days.

Now start the second loop by subtracting 6 from the current value of i. i will always be a multitude of 7. So by subtracting 6 you get the first day in that week. Store that result in j.

Then for the evaluation part of the loop, check if j is equal or lower than i, we never want to go higher than the last day in the current week. Also check if j is equal or lower than numberOfDays. You don't want to add more days than you indicated.

Add each day to the days array. Then after the loop add the days array to daysInWeek. Now you got this nested array structure.

const weeks = [];
const daysInWeek = [];
numberOfDays = 35;

for (let i = 1; i <= numberOfDays; i ++) {
  if (i % 7 === 0){
    weeks.push(i)
    
    const days = [];
    
    for (let j = i - 6; j <= i && j <= numberOfDays; j++) {
      days.push(j);
    }
    
    daysInWeek.push(days);
  }  
}

console.log(daysInWeek);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Your loop is wrong. Please try this also.

Within a single loop we can create both all arrays and add them together like this.

const weeks = [];
    const daysInWeek = [];
    numberOfDays = 35;
    
    for (let i = 1; i <= numberOfDays; i ++) {
      const weekid = parseInt(((i-1)/7));  // used i-1 because in the loop i started as 1. if we use i/7 we loose a day record from the first week. Test it.
      if(!weeks.includes(weekid) ){
        weeks.push(weekid);
      }
      let tempArray = [];
    
      if(daysInWeek[weekid] === undefined){
        daysInWeek[weekid] = [];
      }
      daysInWeek[weekid].push(i);
    
    }
    console.log(daysInWeek);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Math.floor((i - 1) / 7) give you the key of the table to push your days

<script type="text/javascript">
const weeks = [];
const daysInWeek = [[]];
numberOfDays = 35;

for (let i = 1; i <= numberOfDays; i ++) {
  if (i % 7 === 0){
    weeks.push(i)
    daysInWeek.push([])
  }
  daysInWeek[Math.floor((i - 1) / 7)].push(i)
}

console.log(daysInWeek);
</script>
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