Deleting Nested Property in JavaScript Object

Deleting nested property in javascript object

Yes. That would work.

delete tenants['first']['expired']; or delete tenants.first.expired;.

If you are deleting it only because you wanted to exclude it from JSON.stringify(), you can also just set it to undefined, like tenants['first']['expired'] = undefined;

Javascript delete nested object properties by name

const myObj =  {
"name":"John",
"deleteMe":30,
"cars":{
"car1":"Ford",
"car2":"BMW",
"deleteMe":"Fiat",
"wheels":{
"one":"Round",
"two":"Square",
"deleteMe":"Fiat"
}
}
}

const recursiveRemoveKey = (object, deleteKey) => {
delete object[deleteKey];

Object.values(object).forEach((val) => {
if (typeof val !== 'object') return;

recursiveRemoveKey(val, deleteKey);
})
}

recursiveRemoveKey(myObj, 'deleteMe');

console.log(myObj);

How to remove object properties from nested arrays of objects?

Instead of deleting, why not just return an object without the properties you want to remove.

You could destructure the properties you want to remove and then collect other properties in a variable using the rest parameters syntax. After this, you just need to return the variable which contains all the properties other than the ones you want to remove.

const modifiedItems = this.items.map(
({ created, removed, ...rest }) => rest
);

Following code snippet shows an example:

const arr = [
{ removed: 1, created: 1, a:2, b: 2, c: 3},
{ removed: 1, created: 1, a:2, b: 2, c: 3},
{ removed: 1, created: 1, a:2, b: 2, c: 3},
];

const modifiedItems = arr.map(
({ created, removed, ...rest }) => rest
);

console.log(modifiedItems);

how to delete nested properties in javascript

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)

Remove object property from the nested object in the array

You want to be using the delete operator.

I took the liberty of expanding on your code a little to show you how using the key of the object in a forEach loop is more efficient than doing a key / value pair comparison. Also note that I am leaving the original books object intact, this is important for unit testable code and data integrity, but you can change/implement as you need.

let books = [
{
language: 'spanish',
books: {
book_1: 'book1_spanish',
book_2: 'book2_spanish',
book_3: 'book3_spanish'
}
},
{
language: 'italian',
books: {
book_1: 'book1_italian',
book_2: 'book2_italian',
book_3: 'book3_italian'
}
}
];

let targetBook = { book_1: 'book1_spanish' };
let language = 'spanish';
let target = Object.keys(targetBook)[0]; // we only need the 'key' of the targetBook
let result;

function removeTargetBook(books, lan, target) {
books.forEach(elem => { // some instead of forEach works here as well
elem.language === lan && (delete elem.books[target]);
});

return books;
}
// multiple tests for correctness
result = removeTargetBook(books, language, 'fool proof test');
console.log(result);
result = removeTargetBook(books, language, target);
console.log(result);
result = removeTargetBook(books, language, 'book_2');
console.log(result);
result = removeTargetBook(books, language, 'book_3');
console.log(result);

Delete nested JavaScript object

You need a recursive function to delete the property.

var myobj = {  "children": [    {      "name": "albuterol ",      "children": [        {          "name": "albuterol  - fluticasone ",          "children": [            {              "name": "prednisone ",              "children": [                {                  "name": "dexamethasone ",                  "children": [],                  "size": 1,                  "colname": "CONCEPT_NAME.4"                }              ],              "size": 3,              "colname": "CONCEPT_NAME.3"            }          ],          "size": 4,          "colname": "CONCEPT_NAME.2"        }]}]} 
function deleteColnameRecursive(obj){ delete obj.colname if(obj.children){ for(var i=0;i<obj.children.length;i++) deleteColnameRecursive(obj.children[i]); }}
deleteColnameRecursive(myobj);
console.log(myobj);

Delete property and its values in all the object

You could use a recursive algorithm :