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

184
Vistas
array.push seems to create new array

I am grappling with the way my array.push() works while making paginated API requests in Node.JS.

My API gives 10 results per request by default. The API gives a 'next' URL in the link header if there are more records to be retrieved. So I am using parse-link-header NPM to get this new link and make subsequent requests in a do while loop.

My code makes all necessary requests of assignment submissions for a student, for each course they are enrolled in.

E.g., the student is enrolled in 3 courses. And there are:

  • 19 assignments in course #1
  • 12 assignments in course #2
  • 0 assignments in course #3

I should get 3x arrays with the number of items in the array === the number of assignments in that course. E.g.:

//course 1
['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s']
//course 2
['a','b','c','d','e','f','g','h','i','j','k','l']
//course 3
[]

But for each request that is made, it is dividing it into 10 records per array (until there is no 'next' page).

So instead the arrays look like this when they are returned (simplified for example):

//course 1
['a','b','c','d','e','f','g','h','i','j']
['k','l','m','n','o','p','q','r','s']
//course 2
['a','b','c','d','e','f','g','h','i','j']
['k','l']
//course 3
[]

I know I am just doing something silly but am presently unable to figure it out.

My code:

    async function getAssignments(courses, student){
      //array to hold student assignment submissions
     const subs = [];
      let req;
      let parsed;
        for (var i=0; i<courses.length; i++){
          let url = `https://${auths.canvas.domain}/api/v1/courses/${courses[i].id}/students/submissions?student_ids[]=${student}&include[]=assignment`;
          do {
              req = await canvReq(url);
              subs.push(req.data);
              parsed = parse(req.headers.link);
              if (parsed.next){
                url = parsed.next.url;
              }
            } while (parsed.next);
      }
      return subs;
    }

return await getAssignments(courses, student.id);

Many thanks for any assistance you can provide.

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

0

subs.push(req.data) should be subs.push(...req.data). Using the spread operator (...) will push the individual elements of req.data (rather than the req.data array as a whole) to subs.

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