首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在javascript中按日期范围从对象数组中获取唯一数据

如何在javascript中按日期范围从对象数组中获取唯一数据
EN

Stack Overflow用户
提问于 2021-10-25 15:39:15
回答 2查看 48关注 0票数 0

你好,我需要帮助,我正在尝试从JavaScript中的from_date和to_date对象数组中获取最新数据,但无法获取下面是我的数组

代码语言:javascript
复制
[
    {"id": 408,"customer_id": 2,"bill_no": 381,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 409,"customer_id": 3,"bill_no": 382,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 410,"customer_id": 4,"bill_no": 383,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 411,"customer_id": 6,"bill_no": 384,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 412,"customer_id": 7,"bill_no": 385,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 413,"customer_id": 8,"bill_no": 386,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 414,"customer_id": 9,"bill_no": 387,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 387,"customer_id": 2,"bill_no": 360,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 388,"customer_id": 3,"bill_no": 361,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 389,"customer_id": 4,"bill_no": 362,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 390,"customer_id": 6,"bill_no": 363,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 391,"customer_id": 7,"bill_no": 364,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 392,"customer_id": 8,"bill_no": 365,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 393,"customer_id": 9,"bill_no": 366,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 380,"customer_id": 2,"bill_no": 353,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 381,"customer_id": 3,"bill_no": 354,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 382,"customer_id": 4,"bill_no": 355,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 383,"customer_id": 6,"bill_no": 356,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 384,"customer_id": 7,"bill_no": 357,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 385,"customer_id": 8,"bill_no": 358,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 386,"customer_id": 9,"bill_no": 359,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"}
]

我使用下面的代码来排序/过滤:

代码语言:javascript
复制
const filtered = res.data.reduce((accumulator, current) => { 
    if (accumulator.find(x => x.customer_id === current.customer_id) { 
        return accumulator; } 
    accumulator.push(current); return accumulator; }, []); 
console.log(filtered);

我想要上述数组中的唯一数据,按from和to latest date,我想要如下代码所示的结果

代码语言:javascript
复制
[
{"id": 408,"customer_id": 2,"bill_no": 381,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 409,"customer_id": 3,"bill_no": 382,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 410,"customer_id": 4,"bill_no": 383,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 411,"customer_id": 6,"bill_no": 384,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 412,"customer_id": 7,"bill_no": 385,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 413,"customer_id": 8,"bill_no": 386,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 414,"customer_id": 9,"bill_no": 387,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-25 15:54:35

您可以减少数组,并检查日期是否晚于其他日期。

代码语言:javascript
复制
const
    data = [{ id: 408, customer_id: 2, bill_no: 381, bill_period: "weekly", from_date: "2021-10-10", to_date: "2021-10-16" }, { id: 409, customer_id: 3, bill_no: 382, bill_period: "weekly", from_date: "2021-10-10", to_date: "2021-10-16" }, { id: 410, customer_id: 4, bill_no: 383, bill_period: "weekly", from_date: "2021-10-10", to_date: "2021-10-16" }, { id: 411, customer_id: 6, bill_no: 384, bill_period: "weekly", from_date: "2021-10-10", to_date: "2021-10-16" }, { id: 412, customer_id: 7, bill_no: 385, bill_period: "weekly", from_date: "2021-10-10", to_date: "2021-10-16" }, { id: 413, customer_id: 8, bill_no: 386, bill_period: "weekly", from_date: "2021-10-10", to_date: "2021-10-16" }, { id: 414, customer_id: 9, bill_no: 387, bill_period: "weekly", from_date: "2021-10-10", to_date: "2021-10-16" }, { id: 387, customer_id: 2, bill_no: 360, bill_period: "weekly", from_date: "2021-10-03", to_date: "2021-10-09" }, { id: 388, customer_id: 3, bill_no: 361, bill_period: "weekly", from_date: "2021-10-03", to_date: "2021-10-09" }, { id: 389, customer_id: 4, bill_no: 362, bill_period: "weekly", from_date: "2021-10-03", to_date: "2021-10-09" }, { id: 390, customer_id: 6, bill_no: 363, bill_period: "weekly", from_date: "2021-10-03", to_date: "2021-10-09" }, { id: 391, customer_id: 7, bill_no: 364, bill_period: "weekly", from_date: "2021-10-03", to_date: "2021-10-09" }, { id: 392, customer_id: 8, bill_no: 365, bill_period: "weekly", from_date: "2021-10-03", to_date: "2021-10-09" }, { id: 393, customer_id: 9, bill_no: 366, bill_period: "weekly", from_date: "2021-10-03", to_date: "2021-10-09" }, { id: 380, customer_id: 2, bill_no: 353, bill_period: "weekly", from_date: "2021-09-26", to_date: "2021-10-02" }, { id: 381, customer_id: 3, bill_no: 354, bill_period: "weekly", from_date: "2021-09-26", to_date: "2021-10-02" }, { id: 382, customer_id: 4, bill_no: 355, bill_period: "weekly", from_date: "2021-09-26", to_date: "2021-10-02" }, { id: 383, customer_id: 6, bill_no: 356, bill_period: "weekly", from_date: "2021-09-26", to_date: "2021-10-02" }, { id: 384, customer_id: 7, bill_no: 357, bill_period: "weekly", from_date: "2021-09-26", to_date: "2021-10-02" }, { id: 385, customer_id: 8, bill_no: 358, bill_period: "weekly", from_date: "2021-09-26", to_date: "2021-10-02" }, { id: 386, customer_id: 9, bill_no: 359, bill_period: "weekly", from_date: "2021-09-26", to_date: "2021-10-02" }],
    result = data.reduce((r, o) => {
        if (!r || r[0].from_date < o.from_date) return [o];
        if (r[0].from_date === o.from_date) r.push(o);
        return r;
    }, undefined);

console.log(result);
代码语言:javascript
复制
.as-console-wrapper { max-height: 100% !important; top: 0; }

票数 0
EN

Stack Overflow用户

发布于 2021-10-25 16:41:01

您可以使用Array.filter()仅获取所需的对象。您可以使用Date构造函数将from_dateto_date转换为Date对象,并且可以简单地将其与所需的日期范围进行比较。

代码语言:javascript
复制
const bills = [
    {"id": 408,"customer_id": 2,"bill_no": 381,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 409,"customer_id": 3,"bill_no": 382,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 410,"customer_id": 4,"bill_no": 383,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 411,"customer_id": 6,"bill_no": 384,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 412,"customer_id": 7,"bill_no": 385,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 413,"customer_id": 8,"bill_no": 386,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 414,"customer_id": 9,"bill_no": 387,"bill_period": "weekly","from_date": "2021-10-10","to_date": "2021-10-16"},
    {"id": 387,"customer_id": 2,"bill_no": 360,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 388,"customer_id": 3,"bill_no": 361,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 389,"customer_id": 4,"bill_no": 362,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 390,"customer_id": 6,"bill_no": 363,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 391,"customer_id": 7,"bill_no": 364,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 392,"customer_id": 8,"bill_no": 365,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 393,"customer_id": 9,"bill_no": 366,"bill_period": "weekly","from_date": "2021-10-03","to_date": "2021-10-09"},
    {"id": 380,"customer_id": 2,"bill_no": 353,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 381,"customer_id": 3,"bill_no": 354,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 382,"customer_id": 4,"bill_no": 355,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 383,"customer_id": 6,"bill_no": 356,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 384,"customer_id": 7,"bill_no": 357,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 385,"customer_id": 8,"bill_no": 358,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"},
    {"id": 386,"customer_id": 9,"bill_no": 359,"bill_period": "weekly","from_date": "2021-09-26","to_date": "2021-10-02"}
];

// Your desired date range
const startDate = new Date(2021, 10 - 1, 10);
const endDate = new Date(2021, 10 - 1, 16);

const filteredBills = bills.filter(bill => {
  // Convert to Date object
  const [fromYear, fromMonth, fromDay] = bill.from_date.split("-");
  const [toYear, toMonth, toDay] = bill.to_date.split("-");

  const fromDate = new Date(fromYear, fromMonth - 1, fromDay);
  const toDate = new Date(toYear, toMonth - 1, toDay);

  // Check if dates are within your desired range
  const fromDateWithinRange = fromDate >= startDate && fromDate <= endDate;
  const toDateWithinRange = toDate >= startDate && toDate <= endDate;

  return fromDateWithinRange && toDateWithinRange;
});


console.log(JSON.stringify(filteredBills));

日期文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date

Array.filter()文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

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

https://stackoverflow.com/questions/69710977

复制
相关文章

相似问题

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