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
69.6k15 gold badges107 silver badges133 bronze badges
-
2Does this answer your question? How can I remove a specific item from an array?Kinglish– Kinglish2022年10月04日 18:08:26 +00:00Commented Oct 4, 2022 at 18:08
3 Answers 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;
Sign up to request clarification or add additional context in comments.
Comments
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);
Comments
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);
answered Oct 4, 2022 at 18:15
Mikhael Abdallah
5184 silver badges9 bronze badges
Comments
lang-js