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

161
Visualizações
How to "accumulate" a tab in function in JS

I have a tab that I want to use in a function

const extractLinks = async (url, arrLinks) => {
// do things on arrLinks
return arrLinks;
}

I want to create a tab in the beginning of my program, and call several time the function extractLinks on the same tab, to have one tab with lot of values :

let arrLinks = []; // New tab
for (let cptpages = 1; cptpages < 33; cptpages++) { 
  const URL = 'https://droidsoft.fr/category/tests-android/page/' + cptpages;
  extractLinks(URL, arrLinks);
}

When I do that, if I put a console.log(arrLinks), in the function, it prints the value, but after the for iteration, the tab is clear.

Could you help me ?

My whole code :

// Kindacode.com
const cheerio = require('cheerio');

const got = (...args) => import('got').then(({ default: got }) => got(...args));
// You cannot use "require" with the latest version of got
// If you're using ES Module or TypeScript, just import got like this: import got from 'got'

const extractLinks = async (url, arrLinks) => {
  try {
    // Fetching HTML
    const response = await got(url);
    const html = response.body;

    // Using cheerio to extract <a> tags
    const $ = cheerio.load(html);

    const linkObjects = $('a');
    // this is a mass object, not an array

    // Collect the "href" and "title" of each link and add them to an array
    const links = [];
    linkObjects.each((index, element) => {
      if ($(element).attr('href').startsWith('https://droidsoft.fr/202') && $(element).attr('href').includes('test-')) {
        links.push($(element).attr('href'));
      }
    });
    arrLinks = [...new Set(links)];  
    console.log(arrLinks);
    return arrLinks;
    // do something else here with these links, such as writing to a file or saving them to your database
  } catch (error) {
    console.log(error);
  }
};

// Try it
let arrLinks = []; // Tableau vide à la base
for (let cptpages = 1; cptpages < 33; cptpages++) { // On parcourt les 33 pages de tests-android
  const URL = 'https://droidsoft.fr/category/tests-android/page/' + cptpages;
  extractLinks(URL, arrLinks); // On appelle la fonction pour récuperer les URL de chaque test, de chaque page, avec le tableau initial
}

console.log(arrLinks);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It seems like you're re assigning arrLinks everytime, as seen toward the bottom of the code snippet as: arrLinks = [...new Set(links)];

Instead you'll want to do one of the following:

  • Array.prototype.push -> arrLinks.push([...new Set(links)])
  • Clone, add, and set as new -> arrLinks = [...arrLinks, ...new Set(links)]

Alternatively, you could go a different route entirely:

const extractLinks = async (url) => {
  try {
    // Fetching HTML
    const response = await got(url);
    const html = response.body;

    // Using cheerio to extract <a> tags
    const $ = cheerio.load(html);

    const linkObjects = $('a');
    // this is a mass object, not an array

    // Collect the "href" and "title" of each link and add them to an array
    const links = [];
    linkObjects.each((index, element) => {
      if ($(element).attr('href').startsWith('https://droidsoft.fr/202') && $(element).attr('href').includes('test-')) {
        links.push($(element).attr('href'));
      }
    });
    return links // return the array of links itself
  } catch (error) {
    console.log(error);
  }
};

// Try it
let arrLinks = []; // Tableau vide à la base
for (let cptpages = 1; cptpages < 33; cptpages++) { // On parcourt les 33 pages de tests-android
  const URL = 'https://droidsoft.fr/category/tests-android/page/' + cptpages;
  // now we're keeping the array each time and 
  // simply adding the results of the function to it
  arrLinks.push(await extractLinks(URL))  // may need to wrap loop in async func to use await
}
// If you want arrLinks to be a completely flat array 
// (since it will will be an array of arrays, you can just 
// call the `.flat()` method after you're finished like  so `arrLinks.flat()`
console.log(arrLinks);
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