skip to Main Content

I have an array of orders which has an array of lineItems and the line Items have an array of categories attached to them. I am trying to filter the order array based on category id of the line item but am running into trouble. I did the something similar for filtering by item but the 3rd layer is giving me issues. I am trying to avoid using too many for loops.

My array is:

[
{
    "href": "https://sandbox.dev.clover.com/v3/merchants/6RD8H04A896K1/orders/937K8W59RPEHW",
    "id": "937K8W59RPEHW",
    "currency": "USD",
    "employee": {
        "id": "VH7JB40JRGCAG"
    },
    "total": 1165,
    "paymentState": "OPEN",
    "title": "[email protected] safs",
    "orderType": {
        "id": "YT62H0SSGJF0M"
    },
    "taxRemoved": false,
    "isVat": false,
    "state": "open",
    "manualTransaction": false,
    "groupLineItems": false,
    "testMode": false,
    "createdTime": 1702095585000,
    "clientCreatedTime": 1702095585000,
    "modifiedTime": 1702095597000,
    "lineItems": [
        {
            "id": "F7W4DXKZNA4NP",
            "orderRef": {
                "id": "937K8W59RPEHW"
            },
            "item": {
                "id": "A5JPMS665CQW6"
            },
            "name": "Chicken Supreme Bowl /G,O With Bread",
            "alternateName": "",
            "price": 1099,
            "itemCode": "",
            "note": "",
            "printed": false,
            "createdTime": 1702095585000,
            "orderClientCreatedTime": 1702095585000,
            "exchanged": false,
            "refunded": false,
            "isRevenue": true,
            "categories": [
                {
                    "id": "55DYZ8T2ZVB88",
                    "name": "Bowl",
                    "sortOrder": 20,
                    "deleted": false
                }
            ]
        }
    ]
},
{
    "href": "https://sandbox.dev.clover.com/v3/merchants/6RD8H04A896K1/orders/3T3YN416MASV2",
    "id": "3T3YN416MASV2",
    "currency": "USD",
    "employee": {
        "id": "VH7JB40JRGCAG"
    },
    "total": 4025,
    "paymentState": "OPEN",
    "title": "[email protected] gsrw",
    "orderType": {
        "id": "YT62H0SSGJF0M"
    },
    "taxRemoved": false,
    "isVat": false,
    "state": "open",
    "manualTransaction": false,
    "groupLineItems": false,
    "testMode": false,
    "createdTime": 1702095600000,
    "clientCreatedTime": 1702095600000,
    "modifiedTime": 1702095611000,
    "lineItems": [
        {
            "id": "AJWXHE6JE2W4T",
            "orderRef": {
                "id": "3T3YN416MASV2"
            },
            "item": {
                "id": "VNRDHNRBG5AVM"
            },
            "name": "Baklava 1 Tray",
            "alternateName": "",
            "price": 1499,
            "itemCode": "",
            "note": "",
            "printed": false,
            "createdTime": 1702095600000,
            "orderClientCreatedTime": 1702095600000,
            "exchanged": false,
            "refunded": false,
            "isRevenue": true,
            "categories": [
                {
                    "id": "TB58V8F5HTGYM",
                    "name": "DESSERTS",
                    "sortOrder": 15,
                    "deleted": false
                }
            ]
        },
        {
            "id": "F4VYPXC1X9J1J",
            "orderRef": {
                "id": "3T3YN416MASV2"
            },
            "item": {
                "id": "QXBQAMMZR8M6T"
            },
            "name": "Veggie Fried Rice",
            "alternateName": "",
            "price": 1199,
            "itemCode": "",
            "note": "",
            "printed": true,
            "createdTime": 1702095600000,
            "orderClientCreatedTime": 1702095600000,
            "exchanged": false,
            "refunded": false,
            "isRevenue": true,
            "categories": [
                {
                    "id": "F4HYDJAKE13K0",
                    "name": "FRIED RICE",
                    "sortOrder": 13,
                    "deleted": false
                }
            ]
        },
        {
            "id": "ZAECC4E18MB5T",
            "orderRef": {
                "id": "3T3YN416MASV2"
            },
            "item": {
                "id": "A5JPMS665CQW6"
            },
            "name": "Chicken Supreme Bowl /G,O With Bread",
            "alternateName": "",
            "price": 1099,
            "itemCode": "",
            "note": "grgre",
            "printed": false,
            "createdTime": 1702095600000,
            "orderClientCreatedTime": 1702095600000,
            "exchanged": false,
            "refunded": false,
            "isRevenue": true,
            "categories": [
                {
                    "id": "55DYZ8T2ZVB88",
                    "name": "Bowl",
                    "sortOrder": 20,
                    "deleted": false
                }
            ]
        }
    ]
}]

the category I am trying to filter by is:

{id: "55DYZ8T2ZVB88", name:"Bowl"}

my code for filtering is:

this.orders.filter((order) => order.lineItems.some(lineItem => lineItem.categories.some(category =>category.id === item.id)));

but it isnt filtering and when I debug, i get the following error:

VM67561:1 Uncaught TypeError: Cannot read properties of undefined (reading 'some')

even though the nested arrays exist based on the above data. Is there something i am missing or a better way of approaching this.

before, i was filtering by item id which worked but I need to change to filter by category. How i filtered by item id was:

this.orders.filter((order) => order.lineItems.some(lineItem =>lineItem.item.id === item.id));

2

Answers


  1. const orders = [
    {
        "href": "https://sandbox.dev.clover.com/v3/merchants/6RD8H04A896K1/orders/937K8W59RPEHW",
        "id": "937K8W59RPEHW",
        "currency": "USD",
        "employee": {
            "id": "VH7JB40JRGCAG"
        },
        "total": 1165,
        "paymentState": "OPEN",
        "title": "[email protected] safs",
        "orderType": {
            "id": "YT62H0SSGJF0M"
        },
        "taxRemoved": false,
        "isVat": false,
        "state": "open",
        "manualTransaction": false,
        "groupLineItems": false,
        "testMode": false,
        "createdTime": 1702095585000,
        "clientCreatedTime": 1702095585000,
        "modifiedTime": 1702095597000,
        "lineItems": [
            {
                "id": "F7W4DXKZNA4NP",
                "orderRef": {
                    "id": "937K8W59RPEHW"
                },
                "item": {
                    "id": "A5JPMS665CQW6"
                },
                "name": "Chicken Supreme Bowl /G,O With Bread",
                "alternateName": "",
                "price": 1099,
                "itemCode": "",
                "note": "",
                "printed": false,
                "createdTime": 1702095585000,
                "orderClientCreatedTime": 1702095585000,
                "exchanged": false,
                "refunded": false,
                "isRevenue": true,
                "categories": [
                    {
                        "id": "55DYZ8T2ZVB88",
                        "name": "Bowl",
                        "sortOrder": 20,
                        "deleted": false
                    }
                ]
            }
        ]
    },
    {
        "href": "https://sandbox.dev.clover.com/v3/merchants/6RD8H04A896K1/orders/3T3YN416MASV2",
        "id": "3T3YN416MASV2",
        "currency": "USD",
        "employee": {
            "id": "VH7JB40JRGCAG"
        },
        "total": 4025,
        "paymentState": "OPEN",
        "title": "[email protected] gsrw",
        "orderType": {
            "id": "YT62H0SSGJF0M"
        },
        "taxRemoved": false,
        "isVat": false,
        "state": "open",
        "manualTransaction": false,
        "groupLineItems": false,
        "testMode": false,
        "createdTime": 1702095600000,
        "clientCreatedTime": 1702095600000,
        "modifiedTime": 1702095611000,
        "lineItems": [
            {
                "id": "AJWXHE6JE2W4T",
                "orderRef": {
                    "id": "3T3YN416MASV2"
                },
                "item": {
                    "id": "VNRDHNRBG5AVM"
                },
                "name": "Baklava 1 Tray",
                "alternateName": "",
                "price": 1499,
                "itemCode": "",
                "note": "",
                "printed": false,
                "createdTime": 1702095600000,
                "orderClientCreatedTime": 1702095600000,
                "exchanged": false,
                "refunded": false,
                "isRevenue": true,
                "categories": [
                    {
                        "id": "TB58V8F5HTGYM",
                        "name": "DESSERTS",
                        "sortOrder": 15,
                        "deleted": false
                    }
                ]
            },
            {
                "id": "F4VYPXC1X9J1J",
                "orderRef": {
                    "id": "3T3YN416MASV2"
                },
                "item": {
                    "id": "QXBQAMMZR8M6T"
                },
                "name": "Veggie Fried Rice",
                "alternateName": "",
                "price": 1199,
                "itemCode": "",
                "note": "",
                "printed": true,
                "createdTime": 1702095600000,
                "orderClientCreatedTime": 1702095600000,
                "exchanged": false,
                "refunded": false,
                "isRevenue": true,
                "categories": [
                    {
                        "id": "F4HYDJAKE13K0",
                        "name": "FRIED RICE",
                        "sortOrder": 13,
                        "deleted": false
                    }
                ]
            },
            {
                "id": "ZAECC4E18MB5T",
                "orderRef": {
                    "id": "3T3YN416MASV2"
                },
                "item": {
                    "id": "A5JPMS665CQW6"
                },
                "name": "Chicken Supreme Bowl /G,O With Bread",
                "alternateName": "",
                "price": 1099,
                "itemCode": "",
                "note": "grgre",
                "printed": false,
                "createdTime": 1702095600000,
                "orderClientCreatedTime": 1702095600000,
                "exchanged": false,
                "refunded": false,
                "isRevenue": true,
                "categories": [
                    {
                        "id": "55DYZ8T2ZVB88",
                        "name": "Bowl",
                        "sortOrder": 20,
                        "deleted": false
                    }
                ]
            }
        ]
    }]
    
    
    let desiredCategory = { id: "55DYZ8T2ZVB88", name: "Bowl" }; object to filter
    
    let filteredOrders = orders.filter(order => {
      return order.lineItems.some(lineItem => 
        lineItem.categories.some(category =>
          category.id === desiredCategory.id && category.name === desiredCategory.name
        )
      );
    });
    
    console.log(filteredOrders);
    Login or Signup to reply.
  2. Looks like you’re on the right track. Here is how to complete your code:

    const 
          orders = [ { "href": "https://sandbox.dev.clover.com/v3/merchants/6RD8H04A896K1/orders/937K8W59RPEHW", "id": "937K8W59RPEHW", "currency": "USD", "employee": { "id": "VH7JB40JRGCAG" }, "total": 1165, "paymentState": "OPEN", "title": "[email protected] safs", "orderType": { "id": "YT62H0SSGJF0M" }, "taxRemoved": false, "isVat": false, "state": "open", "manualTransaction": false, "groupLineItems": false, "testMode": false, "createdTime": 1702095585000, "clientCreatedTime": 1702095585000, "modifiedTime": 1702095597000, "lineItems": [ { "id": "F7W4DXKZNA4NP", "orderRef": { "id": "937K8W59RPEHW" }, "item": { "id": "A5JPMS665CQW6" }, "name": "Chicken Supreme Bowl /G,O With Bread", "alternateName": "", "price": 1099, "itemCode": "", "note": "", "printed": false, "createdTime": 1702095585000, "orderClientCreatedTime": 1702095585000, "exchanged": false, "refunded": false, "isRevenue": true, "categories": [ { "id": "55DYZ8T2ZVB88", "name": "Bowl", "sortOrder": 20, "deleted": false } ] } ] }, { "href": "https://sandbox.dev.clover.com/v3/merchants/6RD8H04A896K1/orders/3T3YN416MASV2", "id": "3T3YN416MASV2", "currency": "USD", "employee": { "id": "VH7JB40JRGCAG" }, "total": 4025, "paymentState": "OPEN", "title": "[email protected] gsrw", "orderType": { "id": "YT62H0SSGJF0M" }, "taxRemoved": false, "isVat": false, "state": "open", "manualTransaction": false, "groupLineItems": false, "testMode": false, "createdTime": 1702095600000, "clientCreatedTime": 1702095600000, "modifiedTime": 1702095611000, "lineItems": [ { "id": "AJWXHE6JE2W4T", "orderRef": { "id": "3T3YN416MASV2" }, "item": { "id": "VNRDHNRBG5AVM" }, "name": "Baklava 1 Tray", "alternateName": "", "price": 1499, "itemCode": "", "note": "", "printed": false, "createdTime": 1702095600000, "orderClientCreatedTime": 1702095600000, "exchanged": false, "refunded": false, "isRevenue": true, "categories": [ { "id": "TB58V8F5HTGYM", "name": "DESSERTS", "sortOrder": 15, "deleted": false } ] }, { "id": "F4VYPXC1X9J1J", "orderRef": { "id": "3T3YN416MASV2" }, "item": { "id": "QXBQAMMZR8M6T" }, "name": "Veggie Fried Rice", "alternateName": "", "price": 1199, "itemCode": "", "note": "", "printed": true, "createdTime": 1702095600000, "orderClientCreatedTime": 1702095600000, "exchanged": false, "refunded": false, "isRevenue": true, "categories": [ { "id": "F4HYDJAKE13K0", "name": "FRIED RICE", "sortOrder": 13, "deleted": false } ] }, { "id": "ZAECC4E18MB5T", "orderRef": { "id": "3T3YN416MASV2" }, "item": { "id": "A5JPMS665CQW6" }, "name": "Chicken Supreme Bowl /G,O With Bread", "alternateName": "", "price": 1099, "itemCode": "", "note": "grgre", "printed": false, "createdTime": 1702095600000, "orderClientCreatedTime": 1702095600000, "exchanged": false, "refunded": false, "isRevenue": true, "categories": [ { "id": "55DYZ8T2ZVB88", "name": "Bowl", "sortOrder": 20, "deleted": false } ] } ] }],
          
          filterBy = {id: "55DYZ8T2ZVB88", name:"Bowl"},
          
          fOrders = orders.filter(
              ({lineItems:li}) => li.some(({categories:ca}) => ca.some(
                  ({id,name}) => id === filterBy.id && name === filterBy.name
              ))
          );
          
    console.log( fOrders );
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search