0

How to delete nested properties in javascript.

I have example code below which has nested objects and want delete particular nested object on condition I have added condition below. How to delete the entire object based the condition ?

var tenants = [{
    'first': {
        'name': 'first',
        'expired': 1
    },
    'second': []
},{
'first': {
        'name': 'second',
        'expired': 2
    },
    'second': [
        'name': 'third'
    ]
},{
'first': {
        'name': 'third',
        'expired': 3
    },
    'second': [
        'name': 'third'
    ]
},
]
               tenants.forEach((item)  => {
                        if(item.second.length == 0) {
                            console.log('record found..');                            
                            delete item
                        }   
                        else {
                          Data = item;
                        }                     
               });

How can we achieve this get the Expected result as below:

var tenants = [{
'first': {
        'name': 'second',
        'expired': 2
    },
    'second': [
        'name': 'third'
    ]
},{
'first': {
        'name': 'third',
        'expired': 3
    },
    'second': [
        'name': 'third'
    ]
},
]
2
  • Just filter before you work on it - filter out any objects where second is empty Commented May 5, 2021 at 9:49
  • you have a syntax error you couldn't save key-value pair inside the second array Commented May 5, 2021 at 9:55

3 Answers 3

1

you could use filter for that purpose with your condition

but you have a syntax error in the second property you can't save key-value pairs inside the array so I change it to an array of objects instead

'second': [ // old one
    'name': 'third'
]

'second': [{ // new one
    'name': 'third'
}]

var tenants = [{
  'first': {
    'name': 'first',
    'expired': 1
  },
  'second': []
}, {
  'first': {
    'name': 'second',
    'expired': 2
  },
  'second': [{
    'name': 'third'
  }]
}, {
  'first': {
    'name': 'third',
    'expired': 3
  },
  'second': [{
    'name': 'third'
  }]
}, ];

let filteredTenants = tenants.filter(item => item.second.length)
console.log(filteredTenants)

Sign up to request clarification or add additional context in comments.

Comments

1

First you'll have to correct your array.

var tenants = [{
    'first': {
        'name': 'first',
        'expired': 1
    },
    'second': []
},{
'first': {
        'name': 'second',
        'expired': 2
    },
    'second': [
        {'name': 'third'}
    ]
},{
'first': {
        'name': 'third',
        'expired': 3
    },
    'second': [
        {'name': 'third'}
    ]
}
]

Then just use array.splice to remove the item from array.

tenants.forEach((item,index)  => {
      if(item.second.length == 0) {
           console.log('record found..');                            
           tenants.splice(index,1)
      }   
      else {
         Data = item;
      }                     
});

Comments

0

I assume you are using .length to check the condition your second is an array.

var tenants = [{
    'first': {
        'name': 'first',
        'expired': 1
    },
    'second': []
},{
'first': {
        'name': 'second',
        'expired': 2
    },
    'second': [
        'name', 'third'
    ]
},{
'first': {
        'name': 'third',
        'expired': 3
    },
    'second': [
        'name', 'third'
    ]
}
]

var index_to_delete = tenants.findIndex(function(item){
  return item.second.length == 0;
});

tenants.splice(index_to_delete,1);

console.log(tenants);

2 Comments

this wouldn't work in case of multiple items that have empty second array it will get the first item only with empty array
that's right, it did not work in multiple items with empty second array.

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.