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

307
Visualizações
count age number from string in JavaScript

In my api response I'm getting an array that look like this
['data', 'key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47, key=0Sr4C, age=68, key=CGEqo, age=76, key=IxKVQ, age=79, key=eD221, age=29, key=XZbHV, age=32, key=k1SN5, age=88, key=4SCsU, age=65, key=q3kG6, age=33, key=MGQpf, age=13, key=Kj6xW, age=14, key=tg2VM, age=30, key=WSnCU, age=24, key=f1Vvz, age=46, key=dOS7A, age=72, key=tDojg, age=82, key=nZyJA' ]

What I want is to count the number of ages that are greater than 50. Notice that this data can be extracted from the 1 index of the array which is a String. I'm not sure how do I count the ages from it with that condition. I tried using filter and with startsWith("age") to keep a count of the ages. But it didn't work.

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

0

You can use String.prototype.match() to get the age values and then filter the values greater than 50:

const data = 'key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47, key=0Sr4C, age=68, key=CGEqo, age=76, key=IxKVQ, age=79, key=eD221, age=29, key=XZbHV, age=32, key=k1SN5, age=88, key=4SCsU, age=65, key=q3kG6, age=33, key=MGQpf, age=13, key=Kj6xW, age=14, key=tg2VM, age=30, key=WSnCU, age=24, key=f1Vvz, age=46, key=dOS7A, age=72, key=tDojg, age=82, key=nZyJA';
const ages = data.match(/age=\d*/g).filter(match => Number(match.replace(/\D/g, "")) > 50)

console.log(ages.length);

about 4 years ago · Juan Pablo Isaza Relatório

0

let str = 'key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47, key=0Sr4C, age=68, key=CGEqo, age=76, key=IxKVQ, age=79, key=eD221, age=29, key=XZbHV, age=32, key=k1SN5, age=88, key=4SCsU, age=65, key=q3kG6, age=33, key=MGQpf, age=13, key=Kj6xW, age=14, key=tg2VM, age=30, key=WSnCU, age=24, key=f1Vvz, age=46, key=dOS7A, age=72, key=tDojg, age=82, key=nZyJA'

let re = /age=\d+/g

let result = str.match(re).flatMap(e => e.match(/\d+/)).filter(e => parseInt(e) > 50)
let length = result.length

console.log(result)
console.log(length)

about 4 years ago · Juan Pablo Isaza Relatório

0

One of the approach that comes to my mind is first split the arr at index 1 with ,. Then assuming, first there is a key=someValue variable and then age=someAge, start looping the array at index 1, trim and split the string at that particular index with = and get the parse the age. If age is greater than 50, increase the ageCount variable. Note that, since the age string is at every alternate index, iterate the value skipping the key string indices.

Finally, return the ageCount variable.

Here is the solution:

const arr = [
      'data',
      'key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47, key=0Sr4C, age=68, key=CGEqo, age=76, key=IxKVQ, age=79, key=eD221, age=29, key=XZbHV, age=32, key=k1SN5, age=88, key=4SCsU, age=65, key=q3kG6, age=33, key=MGQpf, age=13, key=Kj6xW, age=14, key=tg2VM, age=30, key=WSnCU, age=24, key=f1Vvz, age=46, key=dOS7A, age=72, key=tDojg, age=82, key=nZyJA ',
    ];

const countAgeBiggerThanFifty = () => {
  const newArr = arr[1].split(',');
  let ageCount = 0;
  for (let i = 1; i < newArr.length; i += 2) {
    const str = newArr[i].trim();
    const ageSplitArr = str.split('=');
    const age = parseInt(ageSplitArr[1]);
    if (age > 50) {
      ageCount += 1;
    }
  }
  return ageCount;
};
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