My current task involves removing specific children of an object based on whether their "size" key is set to 0.
To achieve this, I am utilizing the npm package directory-tree to generate a JavaScript object representation of a chosen directory.
The structure of the object is as follows:
{
"path": "directory",
"name": "directory",
"children": [
{
"path": "directory\\file1.html",
"name": "file1.html",
"size": 147,
"extension": ".html",
"type": "file"
},
...
],
...
}
Now, my objective is to recursively delete every directory with a size of 0.
I have attempted to iterate through the object's children using a self-calling function:
function filterObject(obj){
for(i=0; i<obj.children.length; i++){
if(obj.children[i].type == "directory"){
if(obj.children[i].size == 0){
delete obj.children[i]
}
else {
filterObject(obj.children[i])
}
}
}
}
However, I encountered an error:
renderer.js:22 Uncaught TypeError: Cannot read property 'type' of undefined
When I modified the code to check if each child is an object before proceeding:
if(typeof obj.children[i] === 'object' && obj.children[i].type == "directory"){...}
This adjustment led to a loop issue, causing the browser to freeze and requiring a restart.