Someone could help me with this problem? I have an object/array
where I put an object with "status = true"
after a specific condition. Now, my problem is, when all children objects are true, I would like to delete these objects and the parent. I will give an example of this issue.
function removeProp(obj, propToDelete,value) {
for (var property in obj) {
if (obj.hasOwnProperty(property)) {
if (typeof obj[property] == "object") {
removeProp(obj[property],propToDelete,value);
} else {
if(property === propToDelete && obj[property] == value){
delete obj[property];
//delete obj;
}
}
}
}
}
var obj =
{
"name":"Bank Branch 1",
"requests":[
{
"date":"2019-10-16 03:18:02",
"req":[
{
"amount":"300",
"coin":2
},
{
"amount":"500",
"coin":5
}
]
},
{
"date":"2019-10-16 03:19:05",
"req":[
{
"amount":"300",
"coin":2,
"status":true
},
{
"amount":"500",
"coin":5
}
]
},
{
"date":"2019-10-16 03:19:20",
"req":[
{
"amount":"22",
"coin":2,
"status":true
},
{
"amount":"111",
"coin":5,
"status":true
}
]
}
]
}
console.log(JSON.stringify(obj));
removeProp(obj,"status",true);
console.log(JSON.stringify(obj));
I expect this one, where I have to remove all objects where the status was true, and the parent "date" too. Look for the "2019-10-16 03:19:05", I don't want to remove it because there is an object that there is no "status true" yet... And "2019-10-16 03:19:20" was removed because all objects were true.
var obj =
{
"name":"Bank Branch 1",
"requests":[
{
"date":"2019-10-16 03:18:02",
"req":[
{
"amount":"300",
"coin":2
},
{
"amount":"500",
"coin":5
}
]
},
{
"date":"2019-10-16 03:19:05",
"req":[
{
"amount":"300",
"coin":2,
"status":true
},
{
"amount":"500",
"coin":5
}
]
}
]
}