简体   繁体   English

根据月份过滤对象列表?

[英]Filter list of Objects according to the month?

I am using graphs, so whenever I choose to show data according to the months, I will have to show data for the each month, I have fields like totalAmount, paidAmount, I should sum data for that month.我正在使用图表,所以每当我选择根据月份显示数据时,我都必须显示每个月的数据,我有诸如 totalAmount、paidAmount 之类的字段,我应该汇总该月的数据。

const myArray = [
  {
    "id": 9,
    "userId": null,
    "invoiceNumber": "biscuitInvoice",
    "billedBy": 1,
    "billedTo": 2,
    "addGst": false,
    "invoiceDate": "2021-05-08T12:05:00",
    "dueDate": "2021-05-21T12:03:00",
    "totalAmount": 11.8,
    "discountSymbol": null,
    "discountPercent": null,
    "subTotal": null,
    "notes": null,
    "signature": null,
    "reachMail": "",
    "reachPhoneNo": null,
    "businessLogo": null,
    "clientName": "Checking Business",
    "businessName": "Chocolate Business",
    "paymentAmount": 140,
    "status": "Created",
    "igst": 1.8,
    "cgst": 0,
    "amount": null,
    "sgst": 0,
    "businessClient": null,
    "businessProfile": null,
    "invoiceAttachments": [],
    "invoiceItems": [],
    "invoiceTerms": []
  },
  {
    "id": 8,
    "userId": null,
    "invoiceNumber": "invq32",
    "billedBy": 1,
    "billedTo": 3,
    "addGst": false,
    "invoiceDate": "2021-04-04T10:10:22",
    "dueDate": "2021-05-13T10:10:00",
    "totalAmount": 354,
    "discountSymbol": null,
    "discountPercent": null,
    "subTotal": null,
    "notes": null,
    "signature": null,
    "reachMail": "",
    "reachPhoneNo": null,
    "businessLogo": null,
    "clientName": "Checking",
    "businessName": "Chocolate Business",
    "paymentAmount": 120,
    "status": "Paid",
    "igst": 54,
    "cgst": 0,
    "amount": null,
    "sgst": 0,
    "businessClient": null,
    "businessProfile": null,
    "invoiceAttachments": [],
    "invoiceItems": [],
    "invoiceTerms": []
  }
]

In that list, I have invoiceDate, one object is of april month and other is of May month.在那个列表中,我有 invoiceDate,一个 object 是 4 月,另一个是 5 月。

Click here what I meant to do,I want this functionality点击这里我的意思是什么,我想要这个功能

How can I do that, any help?我该怎么做,有什么帮助吗?

Here is the function you need:这是您需要的 function:

function getDesiredMonth(data, month) {
    return data.filter((item) => item.invoiceDate.split("-")[1] === month)
}

For example, you can call it like this to get May invoices: getDesiredMonth(myArray, '05')例如,您可以这样调用它来获取 5 月的发票: getDesiredMonth(myArray, '05')

Let me know if that works for you:)让我知道这是否适合你:)

Answering cause I dont have enough rep to comment.回答是因为我没有足够的代表发表评论。

I guess you can follow these steps:我想您可以按照以下步骤操作:

  1. Initialize aggregate variables that you need (basically sum of sgst or whatever) to 0.将您需要的聚合变量(基本上是 sgst 或其他的总和)初始化为 0。
  2. Iterate over list遍历列表
  3. Use library such as luxon or moment to parse invoice date (they're in ISO, so it'll be straightforward)使用诸如 luxon 或 moment 之类的库来解析发票日期(它们在 ISO 中,所以很简单)
  4. Identify month number from parsed object.从解析的 object 中识别月份数。
  5. Aggregate whatever data you need into the variables initiaziled earlier (eg. total_sgst += current_obj.sgst )将您需要的任何数据聚合到之前初始化的变量中(例如total_sgst += current_obj.sgst
  6. ??? ???
  7. Profit?利润?
function filterDataByMonth(data, month){
    return data.filter(i => new Date(i.invoiceDate).getMonth() + 1 === month);
}

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

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