Kolya_Net Kolya_Net - 22 days ago 6
Javascript Question

Remove all elements with no children from an array

I get the data from the server in the form of

flat tree
. Then I transfer data
JsTree
library that builds a tree to me.
Before transmitting data
JsTree
I filter the data and remove the empty elements of type
folder
that do not have children.

Here are some code I got:

dataFilter: function (jsonString) {
function getItemList(item, list) {
if (item === null) {
return null;
}
if (item.parent === "#" && item.type !== "folder") {
return item;
} else {
if (item.type === "folder") {
var itemFolder = _.findWhere(list, { parent: item.id });
if (itemFolder) {
return item;
}
} else {
return item;
}
}
return null;
};
function removeEmptyFolder(list) {
console.log(list);
var treeListTemp = [];
for (var i = 0; i < list.length; i++) {
treeListTemp.push(getItemList(list[i], list));
}
var treeList = [];
for (var j = 0; j < treeListTemp.length; j++) {
var k = getItemList(treeListTemp[j], treeListTemp);
if (k === null) {
removeEmptyFolder(_.filter(treeListTemp,
function(item) {
return item !== null;
}));
} else {
treeList.push(k);
}
}
//console.log(treeList);
return treeList;
};
return JSON.stringify(_.filter(removeEmptyFolder(JSON.parse(jsonString)),
function (item) {
return item !== null;
}));
}


Here is a working example https://jsfiddle.net/nkqgot2a/11/

This code works almost right. But if the last element contains a child, it is not then removed from the list. Element "folder 9" should not appear in the list after the data filtering.

Please tell me how to fix my function so that it deletes all empty folders?

As yet it seems to me that the recursion is caused by an excessive amount of time.

Answer

Check i have made many changes in it

var data = [
        {
            "id": "15",
            "parent": "#",
            "title": "folder 1",
            "type": "folder"
        },
        {
            "id": "16",
            "parent": "15",
            "title": "file 2",
            "type": "file"
        },
        {
            "id": "21",
            "parent": "25",
            "title": "file 6",
            "type": "file"
        },
        {
            "id": "25",
            "parent": "15",
            "title": "folder 10",
            "type": "folder"
        },
        {
            "id": "33",
            "parent": "#",
            "title": "folder 3",
            "type": "folder"
        },
        {
            "id": "34",
            "parent": "33",
            "title": "folder 4",
            "type": "folder"
        },
        {
            "id": "35",
            "parent": "25",
            "title": "file 5",
            "type": "file"
        },
        {
            "id": "36",
            "parent": "#",
            "title": "file 7",
            "type": "file"
        },
        {
            "id": "37",
            "parent": "#",
            "title": "folder 8",
            "type": "folder"
        },
        {
            "id": "38",
            "parent": "#",
            "title": "folder 9",
            "type": "folder"
        },
        {
            "id": "39",
            "parent": "40",
            "title": "folder 11",
            "type": "folder"
        },
        {
            "id": "40",
            "parent": "38",
            "title": "folder 12",
            "type": "folder"
        }
    ];

    function dataFilter(jsonString) {
        function getItemList(item, list) {
            if (item === null) {
                return null;
            }
            if (item.type === "folder") {
                var itemFolder = _.findWhere(list, {parent: item.id});
                if (itemFolder) {
                    return item;
                }
            } else {
                return item;
            }

            return null;
        }
        function removeEmptyFolder(list) {
            var tempItem = null;
            for (var i = 0; i < list.length; i++) {
                tempItem = getItemList(list[i], list);
                if(tempItem == null){
                    break;
                }
            }
            if(tempItem == null){
                list.splice(i, 1);
                removeEmptyFolder(list);
            }

            return list;

        }
        return removeEmptyFolder(jsonString);
    }


    $(document).ready(function () {
        var $beforelist = $('#before_json');
        $.each(data, function () {
            $('<li>' + this.title + '</li>').appendTo($beforelist);
        });
        var $afterlist = $('#after_json');
        $.each(dataFilter(data), function () {
            $('<li>' + this.title + '</li>').appendTo($afterlist);
        });
    });
<pre id="before_json"></pre>
<p>After:</p>
<pre id="after_json"></pre>


<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

Comments