【问题标题】:Array filter by date range按日期范围过滤数组
【发布时间】:2021-05-25 18:05:36
【问题描述】:

您好,我想过滤日期范围内的数据。

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

【问题讨论】:

  • data.map(result =&gt; {}) 没有效果,所以不知道你为什么在那里。此外,if 中的 data 似乎来自 API 调用,但在 else 中,data 声明/分配在哪里?换句话说,你的 if 和 else 做的是完全不同的事情,看起来并不相关。
  • 抱歉,忘记删除我的地图了。编辑了我的问题。
  • 您的函数有startend 作为参数,但随后它们在else 部分中被重新声明为const 符号。参数被忽略。
  • 对不起,我没看到那部分。编辑了我的问题
  • 您的 startend 日期无效:您在日期的日期部分缺少前导 0。此外,end 时间戳实际上在 start 时间戳之前:可能是错字?

标签: javascript arrays filter


【解决方案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();

【讨论】:

  • @PinkyPromise 正如您在“运行代码 sn-p”中看到的那样,它可以工作。这可能意味着获取和解析的数据与您发布的数据不同。
  • @PinkyPromise 如果没有更多详细信息,我就忍不住了。您提供了示例数据并使用示例数据起作用。对于您未提供的数据,我无能为力。
  • 您有两个console.log 语句。因此你有两个输出。
  • @PinkyPromise 您是否尝试单击“运行代码 sn-p”按钮?如果我的代码有效而您的代码无效,则我们的代码之间存在差异。如果不向我展示您的代码,我将无能为力。我只知道您问题中的代码和数据,并使用这些信息创建了一个可行的解决方案。
  • @PinkyPromise 了解 Stackoverflow 和论坛之间的区别很重要。我们的目标不是帮助单个用户,而是建立一个像 wiki 这样的知识库来帮助许多用户。
【解决方案2】:

鉴于您的数据,您的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'))

【讨论】:

  • 我应该把它放在我的 Object.values 之上吗?不能让它工作。
  • 我要开始没有定义。
  • 在哪里?我的代码 sn-p 是可运行的,它运行:]
  • 在我当前的代码中我无法实现它。关于上面的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
相关资源
最近更新 更多