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

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

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 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