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

100
Visualizações
getting length of array for each element

how can i get forEach elements lengths which was checked in my if else statement?

stackblitz

this.ages = [18, 20, 1];

this.ages.forEach((a) => {
  //infant
  if (a >= 0 && a <= 1) {
    this.id = 1;
    this.travel.listOfTravellerCountPerAgeRange.push({
      travellerAgeRangeId: this.id,
      travellerCount: a.length,
    });
  }

  //adult
  else if (a >= 12 && a <= 59) {
    this.id = 3;
    this.travel.listOfTravellerCountPerAgeRange.push({
      travellerAgeRangeId: this.id,
      travellerCount: a.length,
    });
  }
});
console.log(this.travel);

also i want to push with same travellerAgeRangeId only once but it is pushing twice.

output i get from console.log is:

listOfTravellerCountPerAgeRange: [  
{travellerAgeRangeId: 3, travellerCount: undefined},
{travellerAgeRangeId: 3, travellerCount: undefined},  
{travellerAgeRangeId: 1, travellerCount: undefined} ]

output i wanna get:

 listOfTravellerCountPerAgeRange: [  
 {travellerAgeRangeId: 3, travellerCount: 2}, 
 {travellerAgeRangeId: 1, travellerCount: 1} ]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Updated

Although your approach is inappropriate for doing this but somehow i solved your problem; DEMO

Globally declare your variable

infant = 0;
child = 0;
adult = 0;

Suppose you have array of ages like this

this.ages = [18, 20, 1, 11, 12, 0.2, 10];

Now you run a for loop to count infants, children, and adults.

for (let a = 0; a <= this.ages.length; a++) {
      if (a >= 0 && a <= 1) {
        this.infant = this.infant + 1;
      } else if (a >= 2 && a <= 11) {
        this.child = this.child + 1;
      } else if (a >= 12 && a <= 59) {
        this.adult = this.adult + 1;
      }
    }

and when running the foreach loop you can then reference your global variable like this:

this.ages.forEach((a) => {
      //infant
      if (a >= 0 && a <= 1) {
        this.id = 1;
        this.travel.listOfTravellerCountPerAgeRange.push({
          travellerAgeRangeId: this.id,
          travellerCount: this.infant,
        });
      }
      //child
      else if (a >= 2 && a <= 11) {
        this.id = 2;
        this.travel.listOfTravellerCountPerAgeRange.push({
          travellerAgeRangeId: this.id,
          travellerCount: this.child,
        });
      }
      //adult
      else if (a >= 12 && a <= 59) {
        this.id = 3;
        this.travel.listOfTravellerCountPerAgeRange.push({
          travellerAgeRangeId: this.id,
          travellerCount: this.adult,
        });
      }
    });

Old Version

you are pushing element length in your travellerCount property which is not an array

All you need is get this.ages length

this.ages = [18, 20, 1];

this.ages.forEach((a) => {
  //infant
  if (a >= 0 && a <= 1) {
    this.id = 1;
    this.travel.listOfTravellerCountPerAgeRange.push({
      travellerAgeRangeId: this.id,
      travellerCount: this.ages.length,
    });
  }

  //adult
  else if (a >= 12 && a <= 59) {
    this.id = 3;
    this.travel.listOfTravellerCountPerAgeRange.push({
      travellerAgeRangeId: this.id,
      travellerCount: this.ages.length,
    });
  }
});
console.log(this.travel);
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