简体   繁体   English

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

[英]How to get unique data from array of object by date range in javascript

Hello all I need help I am trying to get the latest data from an array of objects by from_date and to_date in JavaScript I am not able to fetch it below is my array大家好,我需要帮助我正在尝试通过 JavaScript 中的 from_date 和 to_date 从对象数组中获取最新数据我无法在下面获取它是我的数组

[
    {"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"}
]

I want the unique data from the above array by from and to latest date with I want the result like the below code我想要从和到最新日期的上述数组中的唯一数据,我想要像下面的代码一样的结果

[
{"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"},
]

You could reduce the array and check if the date is greater than the other dates.您可以减少数组并检查日期是否大于其他日期。

 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);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

You can use Array.filter() to only get the objects you want.您可以使用Array.filter()仅获取您想要的对象。 You can use the Date constructor to convert your from_date and to_date to Date objects and you can simply compare that against your desired date range.您可以使用 Date 构造函数将您的from_dateto_date转换为 Date 对象,您可以简单地将其与所需的日期范围进行比较。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM