按日期范围过滤数组

时间:2021-02-23 13:01:15

标签: javascript arrays filter

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

{
    "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) {
  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)) 我应该能够获得这些日期范围之间的数据

{
    "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) 时获取日期范围之间的数据

2 个答案:

答案 0 :(得分:1)

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

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 :(得分:1)

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

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

相关问题