首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >筛选带有日期的对象数组

筛选带有日期的对象数组
EN

Stack Overflow用户
提问于 2020-06-18 06:17:53
回答 1查看 109关注 0票数 1

我试图从下面给定的对象数组中获取数据并存储到两个单独的数组中,这些数组满足以下两个条件

来自当前对象数组的

  1. ,我只想获得其月份为当前月份的对象。
  2. 过去7天的另一个数组

我怎么才能做秀?我能得到任何关于约会的提示吗?因为我对它不太擅长?

代码语言:javascript
复制
{
    "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
                }
            ]
        }
    ]
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-18 06:41:45

这里的Date对象将有很大帮助。要获得这个月的订单,您可以尝试如下:

代码语言:javascript
复制
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天里,你可以尝试这样的方法:

代码语言:javascript
复制
orders.filter(order => {
  const orderDate = Date.parse(order.orderDate); // in milliseconds
  const today = Date.now(); // in milliseconds
  const millisecondsInAWeek = 604800000;

  return orderDate > today - millisecondsInAWeek;
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62443554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档