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

129
Visualizações
Creating new Array of objects with an each function

I am building a web scraper using node/puppeteer/cheerio

Using this I am able to console.log the correct strings. I am struggling to understand if this is possible.

$('table[class="ad_tab"]')
  .find("tbody > tr > td")
  .each(function (index, element) {
    if ((index + 1) % 8 == 0) {
      console.log($(element).text());
      console.log("Start New JSON");
    } else {
      console.log($(element).text());
    }

my output are the following strings

1
2
3
4
5
6
7
8
Start New JSON
9
10
11
12
13
14
15
16
Start New JSON

My Intended Result is:

[
 {
  "A": "1",
  "B": "2",
  "C": "3",
  "D": "4",
  "E": "5",
  "F": "6",
  "G": "7"
  "H": "8"
  },
   {
  "A": "9",
  "B": "10",
  "C": "11",
  "D": "12",
  "E": "13",
  "F": "14",
  "G": "15"
  "H": "16"
  }
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can create your case around this solution: Basically replace from your each with reduce.

let input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17];

// input = $('table[class="ad_tab"]').find("tbody > tr > td")

let output = input.reduce((arr, value, i) => {
  let remainder = i % 8;

  if (!remainder) {
    arr.push({})
  };
  let obj = arr[arr.length - 1];
  let char = String.fromCharCode(97 + remainder);
  obj[char] = value;
  return arr;
}, []);

console.log(output)

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