Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!