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

202
Visualizações
Using nested forEach loops to push to an array

I'd love an explanation as to why the result of this else if statement doesn't match my expectation. my if statement works fine, but else if statement pushes the same value to my array. what I am trying to do is that, if by end of the period person is greater than 18 i want childEndDate to be dates of person date when he/she will become 18 years old.

here is my stackblitz

this.childbirthYear.forEach(element => {
  if (periodEndYear - element < 18) {
    this.childEndDate = this.endDate;
  } else if (periodEndYear - element >= 18) {
    this.childBirthDate.forEach(year => {
      this.childEndDate = addYears(new Date(year), 18).toISOString();
    });
  }
  this.final.push(this.childEndDate);
});
console.log(this.final)

person dates i have now is:

this.childBirthDate = [
  '2012-02-16T20:00:00.000Z',
  '2010-05-19T20:00:00.000Z',
];

array returns

["2028-05-19T20:00:00.000Z", "2028-05-19T20:00:00.000Z"]

but it must return

["2030-05-19T20:00:00.000Z", "2028-05-19T20:00:00.000Z"]

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

0

If you want this.childEndDate to hold the year when the person became 18 simply take the element which hold the birthyear and add with 18

dont know why you are using another forEach loop inside the else if

EDIT: in new Date() it expects new Date(year, monthIndex) hence the issue.

this.childbirthYear.forEach((element) => {
  if (periodEndYear - element < 18) {
    this.childEndDate = this.endDate;
  } else if (periodEndYear - element >= 18) {
    this.childEndDate = addYears(new Date(element, 1), 18).toISOString();
  }
  this.final.push(this.childEndDate);
});
console.log(this.final);

Gives output

["2030-01-31T18:30:00.000Z", "2028-01-31T18:30:00.000Z", "2031-11-09T16:09:40.621Z"]
about 4 years ago · Juan Pablo Isaza Relatório

0

I think the following code should do for you...

    const greaterThan18s = this.childBirthDate
    .filter((birthDate: string) => (periodEndYear - new Date(birthDate).getFullYear()) >= 18)
    .forEach((birthDate: string) => this.olderThan18s.push(addYears(new Date(birthDate), 18).toISOString()));

    const lessThan18s = this.childBirthDate
    .filter(birthDate => periodEndYear - new Date(birthDate).getFullYear() < 18)
    .forEach((birthDate: string) => this.youngerThan18s.push(this.endDate));

And then arrange olderThan18s and youngerThan18s in a final array whatever way you like.

For example

this.final = [...olderThan18s, ...youngerThan18s]

// or

this.final = [...youngerThan18s, ...olderThan18s]
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