删除对象或设置为null

时间:2014-06-13 00:22:04

标签: javascript jquery recursion

My Fiddle

屏幕上没有任何内容显示在控制台中。

The Rundown: 所以这是我正在进行的项目。基本上我试图将脚本顶部的巨型对象解构为包含所有必需数据的小对象(代码中的wholeObject)。这样我就可以在这个项目的下一步重建它们。

现在这产生了一个无限循环,但我在这里要做的是到达树的(近)底部,并在最近底部存储所需对象的路径,这样我就可以存储整个具有我想要的所有数据的对象。然后,在我这样做之后,我想从更大的对象中删除该对象,以便我再也找不到它并为更大的对象中可能存在的任何对象重复该过程,直到没有任何东西为止,此时我我完成了这个阶段。

现在的问题是我无法弄清楚如何从更大的对象中删除该对象,以便它不断找到相同的对象。我尝试将其设置为null和delete关键字。据我所知,当我在“r.removeLowest”方法中对“obj”中的任何一个进行操作时,它只是将更改应用于obj变量而不是它所引用的内容(树中的对象/更大)对象)。

我将非常感谢任何帮助,我将在大部分时间内完成并定期检查。

下面的测试对象:

//test object
var sample = {"Ocean Event":
                {"2001":
                    {"Pacific":
                        [{"start":10,"end":30,"magnitude":7},{"start":31,"end":39,"magnitude":9},{"start":40,"end":60,"magnitude":5},{"start":80,"end":90,"magnitude":3},],
                        "Atlantic":
                            [{"start":10,"end":30,"magnitude":3},{"start":40,"end":60,"magnitude":7},{"start":70,"end":90,"magnitude":3}],
                        "Indian":
                            [{"start":0,"end":30,"magnitude":7},{"start":35,"end":60,"magnitude":5},{"start":650,"end":90,"magnitude":3}]
                    },
                    "2002":{"Pacific":
                        [{"start":10,"end":30,"magnitude":7},{"start":40,"end":60,"magnitude":5},{"start":80,"end":90,"magnitude":3}],
                        "Atlantic":
                            [{"start":10,"end":60,"magnitude":3},{"start":40,"end":60,"magnitude":7},{"start":70,"end":90,"magnitude":3}],
                        "Indian":
                            [{"start":0,"end":30,"magnitude":7},{"start":35,"end":60,"magnitude":5},{"start":65,"end":90,"magnitude":3}]
                    },
                    "2003":{"Pacific":
                        [{"start":5,"end":30,"magnitude":7},{"start":40,"end":70,"magnitude":5},{"start":80,"end":90,"magnitude":3}],
                        "Atlantic":
                            [{"start":5,"end":30,"magnitude":3},{"start":40,"end":45,"magnitude":7},{"start":70,"end":90,"magnitude":3}],
                        "Indian":
                            [{"start":10,"end":30,"magnitude":7},{"start":35,"end":60,"magnitude":5},{"start":65,"end":90,"magnitude":3}]
                    }
                },
                "Crime Data":
                    {"2001":
                        {"New York":
                            [{"start":10,"end":30,"magnitude":7},{"start":40,"end":60,"magnitude":5},{"start":80,"end":90,"magnitude":3}],
                            "Atlanta":
                                [{"start":10,"end":60,"magnitude":3},{"start":40,"end":60,"magnitude":7},{"start":70,"end":90,"magnitude":3}],
                            "Bangor":
                                [{"start":0,"end":30,"magnitude":7},{"start":35,"end":60,"magnitude":5},{"start":65,"end":90,"magnitude":3}]
                        },
                        "2002":{"New York":
                            [{"start":10,"end":30,"magnitude":7},{"start":40,"end":60,"magnitude":5},{"start":80,"end":90,"magnitude":3}],
                            "Atlanta":
                                [{"start":10,"end":30,"magnitude":3},{"start":40,"end":60,"magnitude":7},{"start":70,"end":90,"magnitude":3}],
                            "Bangor":
                                [{"start":0,"end":30,"magnitude":7},{"start":35,"end":60,"magnitude":5},{"start":65,"end":90,"magnitude":3}]
                        },
                        "2003":{"New York":
                            [{"start":5,"end":30,"magnitude":7},{"start":40,"end":70,"magnitude":5},{"start":80,"end":90,"magnitude":3}, {"start":91,"end":95,"magnitude":8}],
                            "Atlanta":
                                [{"start":5,"end":30,"magnitude":3},{"start":40,"end":45,"magnitude":7},{"start":70,"end":90,"magnitude":3}],
                            "Bangor":
                                [{"start":10,"end":30,"magnitude":7},{"start":35,"end":60,"magnitude":5},{"start":65,"end":90,"magnitude":3}]
                        }
                    }
                }

下面的脚本:

var clone = $.extend(true, {}, sample);

//class
function reduceObjectToComponents (obj) {
var r = this
r.keys = []
r.wholeObjects = []
r.root  = $.extend(true, {}, obj)

r.dig = function (node) {
    if(typeof node === 'object'){
        if(Object.keys(node).length>0 && typeof node.start === 'undefined' && node!=null){
            r.keys.push(Object.keys(node)[0])
            r.dig(r.getLatest())
        } else {
            r.storeWhole(node, r.keys)
        }
    }
}

r.storeWhole = function (dataObj, path) {
    var wholeObject = {}
    wholeObject.dimension1 = path[0];
    wholeObject.dimension2 = path[1];
    wholeObject.dimension3 = path[2];
    wholeObject.datum = dataObj;
    console.log(wholeObject)
    r.wholeObjects.push(wholeObject)
    r.removeLowest()
    console.log(r.root)
    console.log(r.keys)
    r.keys = []
    r.dig(r.root)
}

r.removeLowest = function () {
    var obj = r.root
    for (var i = 0; i < r.keys.length; i++) {
        obj = obj[r.keys[i]]
    }
    console.log(obj)
    obj = null
    console.log(obj)
}

r.getLatest = function () {
    var obj = r.root
    for (var i = 0; i < r.keys.length; i++) {
        obj = obj[r.keys[i]]
    }
    return obj
}

r.dig(obj)
}
var t = new reduceObjectToComponents(clone)

1 个答案:

答案 0 :(得分:1)

您有一个无限循环,因为您一直在r.dig(r.root)中调用storeWhole。 我简化了你的代码,它似乎正确地遍历你的结构(你不需要删除任何东西):

r.dig = function (node) {
    if(typeof node === 'object'){
        if(Object.keys(node).length>0 && typeof node.start === 'undefined' && node!=null){
            for(key in node){
                r.keys.push(key);
                r.dig(node[key]);
                r.keys.pop();
            }
        } else {
            r.storeWhole(node, r.keys)
        }
    }
}

r.storeWhole = function (dataObj, path) {
    var wholeObject = {}
    wholeObject.dimension1 = path[0];
    wholeObject.dimension2 = path[1];
    wholeObject.dimension3 = path[2];
    wholeObject.datum = dataObj;
    r.wholeObjects.push(wholeObject)
    console.log(r.keys)
}