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

144
Vistas
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 Respuestas
Responde la pregunta

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