List All users who has birthday in current month - Javascript

Viewed 44

I wanted to list all users who are having birthday in current month in an ascending order.

const user = [
  {
    name: 'xxx',
    dob: '21-09-1988'
  },
  {
    name: 'yyy',
    dob: '30-09-2000'
  },
  {
    name: 'zzz',
    dob: '01-10-1988'
  },
]

Below is my code

const today = new Date();

this.members.forEach((member) => {
  let birthdate = member.dob.split('-')[0];
  let birthmonth = member.dob.split('-')[1];

  console.log(birthmonth);
  if (birthmonth === today.getMonth()+1) {
   this.birthdayMembers.push(member);
  }
});

Please help to resolve this.

3 Answers

You need to loop users array, compare months, push users to resulting array and then sort it as you need. Obviously this is just one of many ways to solve this. Check inline comments:

// Users
const users = [
  {
    name: 'yyy',
    dob: '30-09-2000'
  },
  {
    name: 'xxx',
    dob: '21-09-1988'
  },
  {
    name: 'zzz',
    dob: '01-10-1988'
  }
];

// Get current month
const cm = new Date().getMonth()+1;

// Loop users and compare months
// If month match, push user to filtered array
const fa = [];
for(const u of users) if(cm == u.dob.split("-")[1]) fa.push(u);

// Then sort your array as you wish (i.e ASC)
fa.sort((a, b) => (a.dob > b.dob ? 1 : -1));

// Test result
console.log(fa);

You need to combine two array methods: filter and sort.

Also I have some advices for code snippet that you shared:

  • Don't use "let" for variables which values will not be changed, use "const" instead
  • Use desctructuring for getting values from arrays or objects

So here is code that would solve your problem:

const user = [
  {
    name: 'yyy',
    dob: '30-09-2000'
  },
  {
    name: 'xxx',
    dob: '21-09-1988'
  },
  {
    name: 'zzz',
    dob: '01-10-1988'
  },
]

const today = new Date();

const currentMonth = today.getMonth() + 1;

const usersWithBirthdayThisMonth = user
.filter((user) => {
    const [, birthMonth] = user.dob.split('-');
  
  return parseInt(birthMonth) === currentMonth
})
.sort((userA, userB) => {
    const [birthDayA] = userA.dob.split('-');
  const [birthDayB] = userB.dob.split('-');
  return parseInt(birthDayA) - parseInt(birthDayB);
})

console.log(usersWithBirthdayThisMonth)

You just need to make array of current month, and then sort it

const user = [{
        name: 'qqq', dob: '31-09-1988'
    }, {
        name: 'xxx', dob: '21-09-1988'
    }, {
        name: 'yyy', dob: '30-09-2000'
    }, {
        name: 'zzz', dob: '01-10-1988'
    }, {
        name: 'uuu', dob: '01-09-1988'
    },]
    
    const currentMonth = new Date().getMonth() + 1;
    let result = [];
    user.forEach((member) => {
        let birthdate = member.dob.split('-')
        if (birthdate[1] == currentMonth) {
            result.push(member);
        }
    });
    result.sort((a, b) => {
        if (a.dob.split('-')[0] > b.dob.split('-')[0]) return 1; else if (a.dob.split('-')[0] < b.dob.split('-')[0]) return -1; else return 0;
    });
    console.log(result)
Related