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
69.6k15 gold badges107 silver badges133 bronze badges
asked Oct 4, 2022 at 18:07
1

3 Answers 3

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;

answered Oct 4, 2022 at 18:10
Sign up to request clarification or add additional context in comments.

Comments

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);
answered Oct 4, 2022 at 18:58

Comments

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

answered Oct 4, 2022 at 18:15

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.