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

149
Visualizações
Beginner : Loop with condition in table reactJS

I'm trying to make a "weekly planning" in reactJS, i'm a beginner, so sorry if you see any mistakes.

I have an array of events, like this :

  var events = [
{
  date : moment("14-03-2022", "DD-MM-YYYY").format("ddd D MMM YYYY"),
  name : "EVENT MONDAY 13 2022"
},
{
  date : moment("16-03-2022", "DD-MM-YYYY").format("ddd D MMM YYYY"),
  name : "EVENT WEDNESDAY 2022"
}];

I created a condition to see if the event date is the same of the week date and i put this in a table like this :

        <table className="table table-bordered text-center">
      <thead>
        <tr className="bg-light-gray">
          {week.map((wk) => (<th class="text-uppercase">{wk}</th>))}
        </tr>
      </thead>
      <tbody>
        <tr>
          {week.map((wk) => (
              events.map((ev) =>(
                wk === ev.date ?
                <td>{ev.name}</td>
                :
                <td></td>
              ))
          ))}
        </tr>
      </tbody>
    </table>

But it doesn't work because it creates too much of "td" elements, and i don't know how to put the event name below the correct date...

Can someone help me there?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

consider what a table looks like when you need to render it. First, you need to "make rows" and then inside each row, you need to "make columns". Since you're only doing one week, it pretty much gives you the first task for free, there is only one row.

As for the second part, rendering columns, you need to prepare your data better. Organize the events by the day of when they are happening. I will take this as a simple task with events only being on one specific date as you posted, if you wanted events to span multiple days, the code would obviously be different.

First, imaging what is the desired output:

var events = {
  [date]: [eventName, eventName, eventName],
  [date]: [eventName],
  [date]: [],
}

This shows a structure were a day can hero zero, one or more events happening. It's also possible to use undefined instead of [] for the "zero" case. This allows you to easily render:

{week.map((wk) => (
  <td key={wk}>
     {events[wk] ? events[wk].join(', ') : null}
  </td>
)}

Now, what this does is that when rending a day of the week, it prints a list of events into that cell, if there are any. This is simplified for sure, but it shows what to do.

Finally, you need to modify your data to match the final data:

var events = [
  {
    date: moment("14-03-2022", "DD-MM-YYYY").format("ddd D MMM YYYY"),
    name: "EVENT MONDAY 13 2022"
  },
  {
    date: moment("16-03-2022", "DD-MM-YYYY").format("ddd D MMM YYYY"),
    name: "EVENT WEDNESDAY 2022"
  }
];

var eventsMapped = {}
var event
for (event of events) {
  if (!eventsMapped[event.date]) {
    eventsMapped[event.date] = []
  }
  eventsMapped[event.date].push(event.name)
}

You can simply do this inside the render function. For small arrays, it should be fine. If you get a lot of events, you can consider optimizing it later.

Then you can use eventsMapped to render where it was events above.

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