按大于date.now的日期过滤对象数组

时间:2019-06-25 21:14:03

标签: javascript arrays date object ecmascript-6

我在使用箭头功能和按日期值过滤对象数组时遇到一些问题。

我尝试过滤它,但是它返回完整数组,并且没有按要求工作。

最后,我必须返回项目-过期日期大于今天的数组中的对象。必须是箭头功能

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 个答案:

答案 0 :(得分:0)

这是按字典顺序进行的比较-不是您想要的。只需将两个Form( key: _formKey, child: GridView.count( crossAxisCount: 2, crossAxisSpacing: 0, padding: const EdgeInsets.all(10.0), children: List.generate(10, (index) { return index.isEven ? Text( 'Item $index', style: Theme.of(context).textTheme.headline, ) : TextField( decoration: InputDecoration( hintText: "Hint $index", ), ); }), ), ), 进行比较-使用箭头函数意味着您还可以隐式返回

Date
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; }用于将日期字符串设置为有效日期-并非所有浏览器都支持带连字符的日期。

答案 1 :(得分:-1)

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

详细了解filter

对于较旧的浏览器

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

答案 2 :(得分:-1)

您正在比较两个不同的字符串。最好将两个日期对象都转换为时间戳或可以比较的任何值。

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

,您正在尝试将类似“ 03-24-2019”的内容进行比较。可能的流程为:

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)

答案 3 :(得分:-2)

您可以使用Date.parse(date)来获取所传递数据的时间戳,如果与当前日期的差为负,则可以对它们进行过滤。 您的代码应如下所示:

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