最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I filter an array of objects between two dates? - Stack Overflow

programmeradmin2浏览0评论

Aside from the occasional jQuery selectors and element modifications, I'm not too great at javascript. For a problem I'm having, I need to filter out a javascript object by date. I have a structure that looks like this:

Object { version: "3.1.1", released_on: "2016-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 }

I want to find all of the objects from 10 years so between "2016-08-21T00:00:00.000Z" and "2010-08-21T00:00:00.000Z" The problem I'm experiencing is that the released_on: field is a string not a date. Would I need to create a new Date() object convert it into a string, and then use .filter or would I do the opposite, convert the string into a date and then filter. Has anyone tried something like this before?

Aside from the occasional jQuery selectors and element modifications, I'm not too great at javascript. For a problem I'm having, I need to filter out a javascript object by date. I have a structure that looks like this:

Object { version: "3.1.1", released_on: "2016-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 }

I want to find all of the objects from 10 years so between "2016-08-21T00:00:00.000Z" and "2010-08-21T00:00:00.000Z" The problem I'm experiencing is that the released_on: field is a string not a date. Would I need to create a new Date() object convert it into a string, and then use .filter or would I do the opposite, convert the string into a date and then filter. Has anyone tried something like this before?

Share Improve this question edited Sep 15, 2016 at 16:04 Roman Kiselenko 44.4k9 gold badges98 silver badges109 bronze badges asked Sep 15, 2016 at 16:01 Dan RubioDan Rubio 4,90711 gold badges55 silver badges115 bronze badges 2
  • 1 new Date().getTime() will give you a number that will be easier to work with – Daniel Lizik Commented Sep 15, 2016 at 16:03
  • put this in browser console console.log(new Date("2016-08-21T00:00:00.000Z")) will see that your date strings are perfectly valid. Can even use comparison operators directly on date objects – charlietfl Commented Sep 15, 2016 at 16:25
Add a comment  | 

2 Answers 2

Reset to default 18

You might do as follows;

var data = [{ version: "3.1.1", released_on: "2016-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 },
            { version: "3.1.1", released_on: "2011-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 },
            { version: "3.1.1", released_on: "2009-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 },
            { version: "3.1.1", released_on: "2006-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 },
            { version: "3.1.1", released_on: "2013-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 },
            { version: "3.1.1", released_on: "2017-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 },
            { version: "3.1.1", released_on: "2015-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 },
           ],
      ed = new Date("2016-08-21T00:00:00.000Z").getTime(),
      sd = new Date("2010-08-21T00:00:00.000Z").getTime(),
  result = data.filter(d => {var time = new Date(d.released_on).getTime();
                             return (sd < time && time < ed);
                            });
console.log(result);

export const filterBetweenDates = (data, setState, fromDate, toDate) => {
  try {
    setState(data.filter(({ date }) => date >= fromDate && date <= toDate));
  } catch (error) {
    console.log('error while filtering by dates', error.message);
  }
};
发布评论

评论列表(0)

  1. 暂无评论