Home > other >  Get Users with same age
Get Users with same age

Time:10-05

I have a users list which have firstname and age. I want to filter out the users which have same age. Age will not be known , it will come dynamically , so cannot do filter(age > 26)

const users = [
        {firstName: 'Rohan', age: 26},
        {firstName: 'Ronny', age: 22},
        {firstName: 'Ronnie', age: 23},
        {firstName: 'RohanB', age: 26},
    ]

CodePudding user response:

Use reduce() we can group user have the same age together,then use filter() we can get users have the same age

const users = [
        {firstName: 'Rohan', age: 26},
        {firstName: 'Ronny', age: 22},
        {firstName: 'Ronnie', age: 23},
        {firstName: 'RohanB', age: 26},
    ]
    
let result = users.reduce((a,v) => {
  let obj = a.find(i => i.age == v.age)
  if(obj){
     obj.firstName.push(v.firstName)
   }else{
     a.push({'firstName':[v.firstName],'age':v.age})
   }
  return a
},[]).filter(u => u.firstName.length > 1)

console.log(result)

CodePudding user response:

First, reduce them to dict containing arrays of same age. Then go forEach key in dict and return only those that are single.

const users = [
        {firstName: 'Rohan', age: 26},
        {firstName: 'Ronny', age: 22},
        {firstName: 'Ronnie', age: 23},
        {firstName: 'RohanB', age: 26},
    ]

var groupByRemoveMultiple = function(xs, key) {
  let gb = xs.reduce(function(rv, x) {
    (rv[x[key]] = rv[x[key]] || []).push(x);
    return rv;
  }, {});

  let res = [];
  Object.keys(gb).forEach(function(key, index) {
    if (gb[key].length === 1) {
      res.push(gb[key]);
    }
  });

  return res;
};

arr = groupByRemoveMultiple(users, 'age');
console.log(arr);

CodePudding user response:

Your question is not clear but this should return users with age dynamically

const users = [
        {firstName: 'Rohan', age: 26},
        {firstName: 'Ronny', age: 22},
        {firstName: 'Ronnie', age: 23},
        {firstName: 'RohanB', age: 26},
    ]
function filterAge(arr, uAge) {
const newArray = [];
for(let i = 0; i < arr.length; i  )
{
  let userAge = arr[i].age;
  if(userAge === uAge)
  {
   newArray.push(arr[i]);
  }
 
}
return newArray;
}

// const result = filterAge(users, dynamicAge); // dynamicAge should be the variable of the age

const result = filterAge(users, 23);  
console.log(result);

CodePudding user response:

The solution will be to group your Array elements by age property. This is a way for using reduce JS function in order to group items by specific object property:

const users = [
  {firstName: 'Rohan', age: 26},
  {firstName: 'Ronny', age: 22},
  {firstName: 'Ronnie', age: 23},
  {firstName: 'Ronne', age: 23},
  {firstName: 'RohanB', age: 26},
];
console.log("users before grouping: ", users);
const usersGrouped = users.reduce((group, user) => {
  const { age } = user;
  group[age] = group[age] ?? [];
  group[age].push(user);
  return group;
}, {});
console.log("users grouped: ", usersGrouped);

Then you can use output as you want.

CodePudding user response:

How about this?

let users = [
        {firstName: 'Rohan', age: 26},
        {firstName: 'Ronny', age: 22},
        {firstName: 'Ronnie', age: 23},
        {firstName: 'RohanB', age: 26},
        {firstName: 'RohanC', age: 26},
        {firstName: 'RohanD', age: 22},
        {firstName: 'RohanE', age: 23},
        {firstName: 'RohanF', age: 24},
        {firstName: 'RohanG', age: 25}
    ]

const dic = users.map(a => a["age"])
    .reduce((c, el)=>{c[el] = (c[el]||0) 1; return c}, {});
console.log(dic);

Object.keys(dic).forEach((k) => {
    if(dic[k] !== 1){
        users = users.filter(a => a["age"] != k);
    }
});
console.log(users);

  • Related