2

I am trying to fetch the data and store into 2 separate arrays from given below array of objects which meet following two conditions

  1. From the current Array of objects, I want to only get the objects whose month is the current month.

  2. another array for past 7 days

How can I do soo? can I get any tip to work with dates as I am not soo good with it?

{
    "status": "success",
    "results": 10,
    "orders": [
        {
            "orderID": 1,
            "orderStatus": 1,
            "purAmt": 1000,
            "orderDate": "2020-06-14T03:23:20.000Z",
            "fullName": "velocity",
            "email": "[email protected]",
            "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": "[email protected]",
            "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": "[email protected]",
            "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": "[email protected]",
            "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": "[email protected]",
            "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": "[email protected]",
            "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": "[email protected]",
            "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": "[email protected]",
            "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
                }
            ]
        }
    ]
}
6
  • to get Month make use of getMonth() something like this: new Date().getMonth() Commented Jun 18, 2020 at 6:26
  • but how to do with this array and get objects with the current month only @gorak Commented Jun 18, 2020 at 6:29
  • 1
    Like this: obj.orders.filter(({orderDate})=>new Date(orderDate).getMonth()==new Date().getMonth()) Commented Jun 18, 2020 at 6:29
  • and for orders of last 7 days ? @gorak Commented Jun 18, 2020 at 6:33
  • Take your time and solve the second one by yourself, here you will need getDate() method Commented Jun 18, 2020 at 6:35

1 Answer 1

1

The Date object here will help a lot. To get orders this month you could try something like:

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;
})

And to get in the last 7 days you could try something like:

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

  return orderDate > today - millisecondsInAWeek;
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.