从 Javascript 对象中删除项目的递归方式 [英] Recursive way of removing an item from a Javascript Object

查看:19
本文介绍了从 Javascript 对象中删除项目的递归方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 javascript 程序中有类似于树结构的东西,我想从中删除一个子项,我使用递归方法遍历树,但我一直坚持从树中删除一个节点.

I have something similar to a tree structure in my javascript program and I want to remove a child from it, I used a recursive approach to traverse the tree but I am quite stuck in deleting a node from the tree.

相反,我会得到一个 ID 作为参数,我应该根据 child.metadata.id 来检查它,如果它与 ID 匹配,则将其删除.

In contrast, I will get an ID as a parameter and I should check it against the child.metadata.id and delete the child if it maches with the ID.

注意:我的图中有某些类型的子节点,它在那里划分它基于其类型真"或假"的路径,即如果元数据中的子节点为 if/empty.

Note : I have certain type of child nodes in my graph where it divides it path based on its type 'true' or 'false' that is if the type of the child node in the metadata is if/empty.

这是示例树结构.

 var graph =
{
    "metadata": {"id": "sms_in", "type": "api", "optionsDivId": "div_sms_in", "options": {}},
    "data": {
        "true": {
            "isEmpty1": {
                "metadata": {
                    "id": "isEmpty1",
                    "type": "empty",
                    "optionsDivId": "div_isEmpty1",
                    "options": {}
                },
                "data": {
                    "true": {
                        "sms1": {
                            "metadata": {
                                "id": "sms1",
                                "type": "api",
                                "optionsDivId": "div_sms1",
                                "options": {}
                            }, "data": {"true": {}, "false": false}
                        }
                    },
                    "false": {
                        "dbInsert1": {
                            "metadata": {
                                "id": "dbInsert1",
                                "type": "dbInsert",
                                "optionsDivId": "div_dbInsert1",
                                "options": {}
                            },
                            "data": {
                                "true": {
                                    "sms2": {
                                        "metadata": {
                                            "id": "sms2",
                                            "type": "api",
                                            "optionsDivId": "div_sms2",
                                            "options": {}
                                        }, "data": {"true": {}, "false": false}
                                    }
                                }, "false": false
                            }
                        }
                    }
                }
            }
        }, "false": false
    }
};

这是我的遍历函数

var traverse = function (current) {



    if( current == 'undefined' ) return ;

    var currentChildId = current['metadata']['id'];
    var currentChildType = current['metadata']['type'];



    console.log('visiting : ', currentChildId);


    if (currentChildType == 'if' || currentChildType == 'empty') {

        for(var childKeyType in current['data']){

            for( var childKey in current['data'][childKeyType]){

                var child = current['data'][childKeyType][childKey];

                traverse(child);

            }

        }


    } else {

        for (var childKey in current['data']['true']) {

            var child = current['data']['true'][childKey];

            traverse(child);

        }

    }


};

谁能帮我完成删除功能?

Can someone help me to complete the delete function ?

function popChild(current, childId){
    if( current == 'undefined' ) return ;

    var currentChildId = current['metadata']['id'];
    var currentChildType = current['metadata']['type'];

    if(currentChildId == childId){
        delete current;
        return;
    }


    console.log('visiting : ', currentChildId);


    if (currentChildType == 'if' || currentChildType == 'empty') {

        for(var childKeyType in current['data']){

            for( var childKey in current['data'][childKeyType]){

                var child = current['data'][childKeyType][childKey];

                popChild(child, childId, );

            }

        }


    } else {

        for (var childKey in current['data']['true']) {

            var child = current['data']['true'][childKey];

            popChild(child, childId);

        }

    }
}

推荐答案

考虑使用 JSON.stringify 作为迭代对象的一种方式,使用 replacer 参数:

Consider using JSON.stringify as a way to iterate through your object, using the replacer argument:

function remove_ids(object, id) {
  return JSON.parse(JSON.stringify(object, function(key, value) {
    if (typeof value !== 'object' || typeof value.metadata !== 'object' ||
        value.metadata.id !== id) return value;
  }));
}

这篇关于从 Javascript 对象中删除项目的递归方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆