Home > Blockchain >  count age number from string in JavaScript
count age number from string in JavaScript

Time:03-27

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.

CodePudding user response:

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);

CodePudding user response:

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)

CodePudding user response:

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;
};

CodePudding user response:

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 results = data[0]
    .split(',')
    .map((item) => item.trim())
    .filter((item) => item.startsWith('age'))
    .map((item) => item.split('='))
    .map(([age, value]) => Number(value))
    .filter((age) => age > 50)
 
console.log(results);

  • Related