首页 > 解决方案 > 过滤带有日期的对象数组

问题描述

我正在尝试从下面给出的满足以下两个条件的对象数组中获取数据并存储到 2 个单独的数组中

  1. 从当前的对象数组中,我只想获取月份为当前月份的对象。

  2. 过去 7 天的另一个数组

我该怎么办?我可以得到任何提示来处理日期,因为我不太擅长它吗?

{
    "status": "success",
    "results": 10,
    "orders": [
        {
            "orderID": 1,
            "orderStatus": 1,
            "purAmt": 1000,
            "orderDate": "2020-06-14T03:23:20.000Z",
            "fullName": "velocity",
            "email": "velocity@gmail.com",
            "phone": "999999",
            "flatNo": "b-863",
            "complex": "tara tra",
            "landmark": "kaskd",
            "street": "asdasd",
            "area": "rob city",
            "city": "asda",
            "products": [
                {
                    "productID": 1,
                    "name": "lassi",
                    "price": 62,
                    "image": "images\\rtoRAOwj4-conn.PNG",
                    "quantity": 5
                },
                {
                    "productID": 2,
                    "name": "curd",
                    "price": 55,
                    "image": "curd.png",
                    "quantity": 9
                }
            ]
        },
        {
            "orderID": 2,
            "orderStatus": 1,
            "purAmt": 1000,
            "orderDate": "2020-06-14T03:24:32.000Z",
            "fullName": "velocity",
            "email": "velocity@gmail.com",
            "phone": "999999",
            "flatNo": "b-863",
            "complex": "tara tra",
            "landmark": "kaskd",
            "street": "asdasd",
            "area": "rob city",
            "city": "asda",
            "products": [
                {
                    "productID": 6,
                    "name": "chicken chilly",
                    "price": 65,
                    "image": "images\\PIwc5RQ7s-conn2.PNG",
                    "quantity": 1
                },
                {
                    "productID": 7,
                    "name": "buteer flyyy",
                    "price": 70,
                    "image": "images\\GvIgYj-lO-conn2.PNG",
                    "quantity": 2
                }
            ]
        },
        {
            "orderID": 4,
            "orderStatus": 1,
            "purAmt": 250,
            "orderDate": "2020-06-15T09:04:45.000Z",
            "fullName": "velocity",
            "email": "velocity@gmail.com",
            "phone": "999999",
            "flatNo": "b-863",
            "complex": "tara tra",
            "landmark": "kaskd",
            "street": "asdasd",
            "area": "rob city",
            "city": "asda",
            "products": [
                {
                    "productID": 1,
                    "name": "lassi",
                    "price": 62,
                    "image": "images\\rtoRAOwj4-conn.PNG",
                    "quantity": 1
                },
                {
                    "productID": 2,
                    "name": "curd",
                    "price": 55,
                    "image": "curd.png",
                    "quantity": 1
                }
            ]
        },
        {
            "orderID": 5,
            "orderStatus": 2,
            "purAmt": 250,
            "orderDate": "2020-05-15T10:33:59.000Z",
            "fullName": "velocity",
            "email": "velocity@gmail.com",
            "phone": "999999",
            "flatNo": "b-863",
            "complex": "tara tra",
            "landmark": "kaskd",
            "street": "asdasd",
            "area": "rob city",
            "city": "asda",
            "products": [
                {
                    "productID": 2,
                    "name": "curd",
                    "price": 55,
                    "image": "curd.png",
                    "quantity": 1
                }
            ]
        },
        {
            "orderID": 6,
            "orderStatus": 2,
            "purAmt": 250,
            "orderDate": "2020-06-15T10:41:53.000Z",
            "fullName": "velocity",
            "email": "velocity@gmail.com",
            "phone": "999999",
            "flatNo": "b-863",
            "complex": "tara tra",
            "landmark": "kaskd",
            "street": "asdasd",
            "area": "rob city",
            "city": "asda",
            "products": [
                {
                    "productID": 2,
                    "name": "curd",
                    "price": 55,
                    "image": "curd.png",
                    "quantity": 1
                }
            ]
        },
        {
            "orderID": 7,
            "orderStatus": 2,
            "purAmt": 250,
            "orderDate": "2020-06-15T10:44:58.000Z",
            "fullName": "velocity",
            "email": "velocity@gmail.com",
            "phone": "999999",
            "flatNo": "b-863",
            "complex": "tara tra",
            "landmark": "kaskd",
            "street": "asdasd",
            "area": "rob city",
            "city": "asda",
            "products": [
                {
                    "productID": 2,
                    "name": "curd",
                    "price": 55,
                    "image": "curd.png",
                    "quantity": 1
                }
            ]
        },
        {
            "orderID": 8,
            "orderStatus": 2,
            "purAmt": 250,
            "orderDate": "2020-06-15T11:00:57.000Z",
            "fullName": "velocity",
            "email": "velocity@gmail.com",
            "phone": "999999",
            "flatNo": "b-863",
            "complex": "tara tra",
            "landmark": "kaskd",
            "street": "asdasd",
            "area": "rob city",
            "city": "asda",
            "products": [
                {
                    "productID": 2,
                    "name": "curd",
                    "price": 55,
                    "image": "curd.png",
                    "quantity": 1
                }
            ]
        },
        {
            "orderID": 9,
            "orderStatus": 1,
            "purAmt": 250,
            "orderDate": "2020-06-15T11:01:50.000Z",
            "fullName": "velocity",
            "email": "velocity@gmail.com",
            "phone": "999999",
            "flatNo": "b-863",
            "complex": "tara tra",
            "landmark": "kaskd",
            "street": "asdasd",
            "area": "rob city",
            "city": "asda",
            "products": [
                {
                    "productID": 2,
                    "name": "curd",
                    "price": 55,
                    "image": "curd.png",
                    "quantity": 1
                }
            ]
        }
    ]
}

标签: javascriptarraysobject

解决方案


这里的Date对象会有很大帮助。要获得本月的订单,您可以尝试以下操作:

orders.filter(order => {
  const orderDate = new Date(order.orderDate);
  const today = new Date();
  const isThisYear = orderDate.getFullYear() === today.getFullYear()
  const isThisMonth = orderDate.getMonth() === today.getMonth();

  return isThisYear && isThisMonth;
})

要在最后 7 天获得,您可以尝试以下操作:

orders.filter(order => {
  const orderDate = Date.parse(order.orderDate); // in milliseconds
  const today = Date.now(); // in milliseconds
  const millisecondsInAWeek = 604800000;

  return orderDate > today - millisecondsInAWeek;
})

推荐阅读