0

My array is as such

 "regions": [
            {
                "Id": 1,
                "code": 1,
                "name": "Trivandrum",
                "Categories": [
                    {
                        "Id": 1,
                        "category_id": 1,
                        "region_id": 1,
                        "ref_count": 2,
                    }
                ],
                "Shops": [
                    {
                        "Id": 33,
                        "is_enabled": true,
                        "is_setup_complete": false,
                        "approval_time": 10,
                        "packaging_time": 30,
                        "erp_ref": "45645655",
                        "priority": 0,
                        "admin_id": 11,
                        "region_id": 1,
                    },
                    {
                        "Id": 34,
                        "is_enabled": true,
                        "is_setup_complete": false,
                        "approval_time": 10,
                        "packaging_time": 30,
                        "erp_ref": "4524645",
                        "priority": 0,
                        "admin_id": 11,
                        "region_id": 1,
                    },
                    
                ]
            }
        ]

I want to completely remove the Shops from the array.How can I do it in JavaScript?

Jens
  • 67,715
  • 15
  • 98
  • 113
  • 2
    Does this answer your question? [How can I remove a specific item from an array?](https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array) – Kinglish Oct 04 '22 at 18:08

3 Answers3

3

You can do this simply with the delete operator which removes a property from an object. In this case, you can use:

delete regions.regions[0].Shops;

Ethan R
  • 338
  • 2
  • 9
0

let regions = [
            {
                "Id": 1,
                "code": 1,
                "name": "Trivandrum",
                "Categories": [
                    {
                        "Id": 1,
                        "category_id": 1,
                        "region_id": 1,
                        "ref_count": 2,
                    }
                ],
                "Shops": [
                    {
                        "Id": 33,
                        "is_enabled": true,
                        "is_setup_complete": false,
                        "approval_time": 10,
                        "packaging_time": 30,
                        "erp_ref": "45645655",
                        "priority": 0,
                        "admin_id": 11,
                        "region_id": 1,
                    },
                    {
                        "Id": 34,
                        "is_enabled": true,
                        "is_setup_complete": false,
                        "approval_time": 10,
                        "packaging_time": 30,
                        "erp_ref": "4524645",
                        "priority": 0,
                        "admin_id": 11,
                        "region_id": 1,
                    },
                    
                ]
            }
        ]

for(let region of regions) {
  delete region.Shops;
 }
 
 console.log(regions);
0

Use delete to get rid of an object, or maybe multiple if you wanted to.

"regions": [
            {
                "Id": 1,
                "code": 1,
                "name": "Trivandrum",
                "Categories": [
                    {
                        "Id": 1,
                        "category_id": 1,
                        "region_id": 1,
                        "ref_count": 2,
                    }
                ],
                "Shops": [
                    {
                        "Id": 33,
                        "is_enabled": true,
                        "is_setup_complete": false,
                        "approval_time": 10,
                        "packaging_time": 30,
                        "erp_ref": "45645655",
                        "priority": 0,
                        "admin_id": 11,
                        "region_id": 1,
                    },
                    {
                        "Id": 34,
                        "is_enabled": true,
                        "is_setup_complete": false,
                        "approval_time": 10,
                        "packaging_time": 30,
                        "erp_ref": "4524645",
                        "priority": 0,
                        "admin_id": 11,
                        "region_id": 1,
                    },
                    
                ]
            }
        ]
delete(region.Shops);
console.log(region);
Random BoY
  • 16
  • 2