Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否根据月份筛选对象列表?_Javascript_Reactjs - Fatal编程技术网

Javascript 是否根据月份筛选对象列表?

Javascript 是否根据月份筛选对象列表?,javascript,reactjs,Javascript,Reactjs,我使用图形,所以每当我选择根据月份显示数据时,我必须显示每个月的数据,我有totalAmount、paidAmount等字段,我应该对该月的数据求和 const myArray = [ { "id": 9, "userId": null, "invoiceNumber": "biscuitInvoice", "billedBy": 1, "bi

我使用图形,所以每当我选择根据月份显示数据时,我必须显示每个月的数据,我有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": []
  }
]
在那个列表中,我有invoiceDate,一个对象是四月,另一个是五月


有什么帮助吗?

以下是您需要的功能:

函数getDesiredMonth(数据,月份){
返回数据.filter((项目)=>item.invoiceDate.split(“-”[1]”)==month)
}
例如,您可以这样调用它以获取五月发票:
getDesiredMonth(myArray,'05')


让我知道这是否适合你:)

回答,因为我没有足够的代表发表评论

我想你可以遵循以下步骤:

  • 将所需的聚合变量(基本上是sgst之和或其他)初始化为0
  • 遍历列表
  • 使用诸如luxon或moment之类的库解析发票日期(它们在ISO中,所以很简单)
  • 从解析的对象中标识月份号
  • 将您需要的任何数据聚合到前面初始化的变量中(例如,
    total\u sgst+=current\u obj.sgst
  • 利润

  • 做什么。。。到底是什么问题?月在哪里?是基于日期吗?是的,基于日期,根据该对象中的invoiceDate Hanks bro,我会尝试让你知道谢谢@Elvaleryn成功了,如果你接受我的答案,我会非常感激,然后尝试让你知道
    function filterDataByMonth(data, month){
        return data.filter(i => new Date(i.invoiceDate).getMonth() + 1 === month);
    }