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

97
Visualizações
Is there a way to combine the function body of this forEach funtion?

So I have this forEach function that replaces the text in a table cell after some data is submittied. I'm replacing three different table cells based on table row and I have those separated out into three different statements.

Like this:

replaceData.forEach((item, i) => {
  $(`.tableData tbody tr:eq(${item.index})`)
    .children(["td:eq(6)"])
    .text(values[i] === null ? "-" : values[i]);
  $(`.tableData tbody tr:eq(${item.index})`)
    .children("td:eq(7)")
    .text(reason[i]);
  $(`.tableData tbody tr:eq(${item.index})`)
    .children("td:eq(8)")
    .text(notes[i] === null ? "-" : notes[i]);
});

I tried combining those three statements like this, but this didn't work. Does anyone know how I can combine these three statements, if possible? Or is the only way to achieve this is by keeping them as their own statement?

$(`.tableData tbody tr:eq(${item.index})`)
  .children(["td:eq(6)", "td:eq(7)", "td:eq(8)"])
  .text([
    values[i] === null ? "-" : values[i],
    reason[i],
    notes[i] === null ? "-" : notes[i]
  ]);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Doing them separately looks like the correct thing to do, putting everything in 1 single line doesn't gain you anything, and loses lots in readability, especially when using lots of ternary operations.

But you could clean up your code here by just creating a reference to the row, instead of repeating it each time.

eg.

replaceData.forEach((item, i) => {
  const row = $(`.tableData tbody tr:eq(${item.index})`);                   
  row.children('td:eq(6)').text(values[i] === null ? '-' : values[i])
  row.children('td:eq(7)').text(reason[i]);
  row.children('td:eq(8)').text(notes[i] === null ? '-' : notes[i])
})

If you have lots of columns, you could even create a simple inline function to make things even easier to follow.

eg.

replaceData.forEach((item, i) => {
  const row = $(`.tableData tbody tr:eq(${item.index})`);                   
  const R = c => row.children(`td:eq(${c})`);
  R(6).text(values[i] === null ? '-' : values[i]);
  R(7).text(reason[i]);
  R(8).text(notes[i] === null ? '-' : notes[i]);
})
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