Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

308
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda