Skip to content
Advertisement

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.

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.

Click here what I meant to do,I want this functionality

How can I do that, any help?

Advertisement

Answer

Here is the function you need:

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

Let me know if that works for you 🙂

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement