首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按日期范围过滤数组

问按日期范围过滤数组
EN

Stack Overflow用户
提问于 2021-02-23 21:01:15
回答 2查看 72关注 0票数 0

嗨,我想在日期范围内过滤我的数据。

代码语言:javascript
复制
{
    "websiteId": "4f8b36d00000000000000001",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 121,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000002",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 13,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000003",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 232,
    "missedChats": 9
},
{
    "websiteId": "4f8b36d00000000000000004",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 9,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000005",
    "date": "2019-04-03T00:00:00.000Z",
    "chats": 0,
    "missedChats": 5
},
{
    "websiteId": "4f8b36d00000000000000006",
    "date": "2019-04-03T00:00:00.000Z",
    "chats": 10,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000007",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 100,
    "missedChats": 1
},
{
    "websiteId": "4f8b36d00000000000000008",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 0,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000007",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 0,
    "missedChats": 1
},
{
    "websiteId": "4f8b36d00000000000000006",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 100,
    "missedChats": 50
}

我当前的实现

代码语言:javascript
复制
function getData(start, end) {
  if (start == null && end == null) {
    fetch(url)
      .then((res) => res.json())
      .then((data) => {

        console.log(
          Object.values(
            data.reduce((acc, el) => {
              if (!(el.websiteId in acc)) {
                acc[el.websiteId] = el;
              } else {
                acc[el.websiteId].chats += el.chats;
                acc[el.websiteId].missedChats += el.missedChats;
              }
              return acc;
            }, {})
          )
        );
      });
  } else {
    var start = new Date('2019-04-01T00:00:00.000Z').getTime();
    var end = new Date('2019-04-05T00:00:00.000Z').getTime();

    result = data.filter((d) => {
      var time = new Date(d.date).getTime();
      return start < time && time < end;
    });
    console.log(result);
  }
}

预期输出

当我运行getData(startDate(2019-4-01),endDate(2019-4-02))时,我应该能够获得这些日期范围内的数据

代码语言:javascript
复制
{
    "websiteId": "4f8b36d00000000000000007",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 100,
    "missedChats": 2
},
{
    "websiteId": "4f8b36d00000000000000006",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 110,
    "missedChats": 50
}

我想在运行函数getDate(startDate,endDate)时获取日期范围内的数据

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-23 22:25:16

您应该将if条件移到.then块中:

​

代码语言:javascript
复制
const data = [{
    "websiteId": "4f8b36d00000000000000001",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 121,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000002",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 13,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000003",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 232,
    "missedChats": 9
},
{
    "websiteId": "4f8b36d00000000000000004",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 9,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000005",
    "date": "2019-04-03T00:00:00.000Z",
    "chats": 0,
    "missedChats": 5
},
{
    "websiteId": "4f8b36d00000000000000006",
    "date": "2019-04-03T00:00:00.000Z",
    "chats": 10,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000007",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 100,
    "missedChats": 1
},
{
    "websiteId": "4f8b36d00000000000000008",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 0,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000007",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 0,
    "missedChats": 1
},
{
    "websiteId": "4f8b36d00000000000000006",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 100,
    "missedChats": 50
}];

function getData(start, end) {
  /*fetch(url)
    .then((res) => res.json())
    .then((data) => {*/
      const summedData =
        Object.values(
          data.reduce((acc, el) => {
            if (!(el.websiteId in acc)) {
              acc[el.websiteId] = el;
            } else {
              acc[el.websiteId].chats += el.chats;
              acc[el.websiteId].missedChats += el.missedChats;
            }
            return acc;
          }, {}));
      if (start && end) {
        result = summedData.filter((d) => {
          const time = new Date(d.date).getTime();
          return start < time && time < end;
        });
        console.log(result);
      } else {
        console.log(summedData);
      }
    /*});*/
}

const start = new Date('2019-04-01T00:00:00.000Z').getTime();
const end = new Date('2019-04-05T00:00:00.000Z').getTime();

getData(start, end);
getData();

​

票数 1
EN

Stack Overflow用户

发布于 2021-02-23 21:14:51

根据您的数据,您的filter方法是正确的,您应该将日期字符串转换为Date并与getTime进行比较。

​

代码语言:javascript
复制
const data = [{
  "websiteId": "4f8b36d00000000000000001",
  "date": "2019-04-01T00:00:00.000Z",
  "chats": 121,
  "missedChats": 0
}, {
  "websiteId": "4f8b36d00000000000000002",
  "date": "2019-04-01T00:00:00.000Z",
  "chats": 13,
  "missedChats": 0
}, {
  "websiteId": "4f8b36d00000000000000003",
  "date": "2019-04-01T00:00:00.000Z",
  "chats": 232,
  "missedChats": 9
}, {
  "websiteId": "4f8b36d00000000000000004",
  "date": "2019-04-01T00:00:00.000Z",
  "chats": 9,
  "missedChats": 0
}, {
  "websiteId": "4f8b36d00000000000000005",
  "date": "2019-04-03T00:00:00.000Z",
  "chats": 0,
  "missedChats": 5
}, {
  "websiteId": "4f8b36d00000000000000006",
  "date": "2019-04-03T00:00:00.000Z",
  "chats": 10,
  "missedChats": 0
}, {
  "websiteId": "4f8b36d00000000000000007",
  "date": "2019-04-01T00:00:00.000Z",
  "chats": 100,
  "missedChats": 1
}, {
  "websiteId": "4f8b36d00000000000000008",
  "date": "2019-04-01T00:00:00.000Z",
  "chats": 0,
  "missedChats": 0
}, {
  "websiteId": "4f8b36d00000000000000007",
  "date": "2019-04-02T00:00:00.000Z",
  "chats": 0,
  "missedChats": 1
}, {
  "websiteId": "4f8b36d00000000000000006",
  "date": "2019-04-02T00:00:00.000Z",
  "chats": 100,
  "missedChats": 50
}];

function getData(start, end) {
  const startTime = new Date(start).getTime();
  const endTime = new Date(end).getTime();

  return data.filter(item => {
    const itemTime = new Date(item.date).getTime();

    return itemTime >= startTime && itemTime <= endTime;
  })
}

console.log(getData('2019-04-01', '2019-04-02'))

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66333633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档