Filter an array of objects by date that > than date.now

Viewed 7023

I'm having some problems with arrow functions and filtering array of objects by value of date.

I've tried to filter it but it returns full array, and did'nt working as it must be.

in the end i've got to return items - objects in array which expiration date is bigger than today. It must be arrow function

const menuItems = [{
    name: 'Hamburger',
    expirationDate: '09-24-2019'
  },
  {
    name: 'Chicken',
    expirationDate: '10-03-2019'
  },
  {
    name: 'Hot-Dog',
    expirationDate: '03-24-2019'
  },
];

let today = new Date();
let dd = today.getDate();
let mm = today.getMonth();
let yyyy = today.getFullYear();
if (dd < 10) {
  dd = '0' + dd;
}

if (mm < 10) {
  mm = '0' + mm;
}
today = mm + '-' + dd + '-' + yyyy;

console.log(today);

const filterByExpiration = (items) => {
  menuItems.filter(function() {
    return items.expirationDate > today;
  })
};
4 Answers

That's doing lexicographical comparison - not what you want. Just compare the two Dates together - and using an arrow function means you can also have an implicit return

const menuItems = [{name:'Hamburger',expirationDate:'09-24-2019'},{name:'Pizza',expirationDate:'03-11-2019'},{name:'Sushi',expirationDate:'03-21-2019'},{name:'Chicken',expirationDate:'10-03-2019'},{name:'Steak',expirationDate:'05-27-2019'},{name:'Hot-Dog',expirationDate:'03-24-2019'}];
const today = new Date();
const filterByExpiration = arr => arr.filter(({ expirationDate }) => new Date(expirationDate.replace(/-/g, "/")) > today);
console.log(filterByExpiration(menuItems));
.as-console-wrapper { max-height: 100% !important; top: auto; }

The replace is to make the date strings into valid dates - not all browsers support hyphenated dates.

const menuItems = [
  {
    name: 'Hamburger',
    expirationDate: '09-24-2019'
  },
  {
    name: 'Pizza',
    expirationDate: '03-11-2019'
  },
  {
    name: 'Sushi',
    expirationDate: '03-21-2019'
  },
  {
    name: 'Chicken',
    expirationDate: '10-03-2019'
  },
  {
    name: 'Steak',
    expirationDate: '05-27-2019'
  },
  {
    name: 'Hot-Dog',
    expirationDate: '03-24-2019'
  },
];


const filterByExpiration = () => {
  const Today = Date.now()
  return menuItems.filter(function (item) {
    return new Date(item.expirationDate).getTime() > Today;
  });
};

filterByExpiration();

Read More About filter

For Older Browsers

var filterByExpiration = () => {
  var Today = new Date(); 
  return menuItems.filter(function (item) {
    var year = Today.getFullYear();
    var month = Today.getMonth()+1;
    var day = Today.getDay();
    var dateYearArray = item.expirationDate.split('-');
    var dateYear = dateYearArray[2];
    var dateMonth = dateYearArray[0];
    var dateDay = dateYearArray[1];
    var isYear = year > dateYear;
    var isMonth = month > dateMonth;
    var isDay = day > dateDay;
    return !(isYear || isMonth || isDay);
  });
};


filterByExpiration();

You are comparing two different string. Its much better to convert both date object in timestamp or any value where you can compare.

new Date(); //gives something like this Wed Jun 26 2019 02:55:59 GMT+0530

and you are trying to compare with '03-24-2019' something like this. Possible flow would be :

const menuItems = [{
    name: 'Hamburger',
    expirationDate: '09-24-2019'
  },
  {
    name: 'Chicken',
    expirationDate: '10-03-2019'
  },
  {
    name: 'Hot-Dog',
    expirationDate: '03-24-2019'
  },
];
const today = new Date().getTime();
const filterByExpiration = menuItems.filter((items)  => {
    return new Date(items.expirationDate).getTime() > today;
  })
console.log(filterByExpiration)

You could use Date.parse(date) to get the timestamp of the passed data and filter them if the difference with the current date is negative. Your code should be something like this:

const menuItems = [
  {
    name: 'Hamburger',
    expirationDate: '09-24-2019'
  },
  {
    name: 'Chicken',
    expirationDate: '10-03-2019'
  },
  {
    name: 'Hot-Dog',
    expirationDate: '03-24-2019'
  },
];

function filterByExpiration (items) {
  let today = new Date();
  let dd = today.getDate();
  let mm = today.getMonth();
  let yyyy = today.getFullYear();
  if (dd < 10) {
    dd = '0' + dd;
  }
  if (mm < 10) {
    mm = '0' + mm;
  }
  today = mm + '-' + dd + '-' + yyyy;
  return menuItems.filter((item) => item.expirationDate > today)
};
Related