我有一个没有确定级别的json树,看起来像这样:
var nodeset = {
"name" : "FORM",
"attributes" : {
"IDFORM" : "59",
"NOMDOC" : "1",
"VERSFORM" : "1.01",
"DATEREPORT" : "10.10.1988"
},
"nodes" : [{
"name" : "PART1",
"persist" : true,
"nodes" : [{
"name" : "FTYPE",
"persist" : true,
"value" : "1",
"weight" : 1
}, {
"name" : "STARTDATE",
"persist" : true,
"value" : "01.10.2011",
"weight" : 1
}, {
"name" : "ENDDATE",
"persist" : true,
"value" : "31.12.2011",
"weight" : 1
}
],
"value" : "31.12.2011",
"weight" : 3
}, {
"name" : "PART2",
"persist" : true,
"nodes" : [{
"name" : "F203",
"persist" : true,
"value" : 12,
"weight" : 1
}, {
"name" : "F204",
"persist" : true,
"value" : 12,
"weight" : 1
}, {
"name" : "STI059DETAIL",
"persist" : false,
"nodes" : [{
"name" : "F1",
"persist" : false,
"value" : "asd",
"weight" : 1
}, {
"name" : "F2",
"persist" : false,
"value" : "asd",
"weight" : 1
}, {
"name" : "F3",
"persist" : false,
"value" : 0,
"weight" : 0
}, {
"name" : "F4",
"persist" : false,
"value" : 0,
"weight" : 0
}
],
"value" : 0,
"weight" : 2
}, {
"name" : "STI059DETAIL",
"persist" : false,
"nodes" : [{
"name" : "F1",
"persist" : false,
"value" : null,
"weight" : 0
}, {
"name" : "F2",
"persist" : false,
"value" : null,
"weight" : 0
}, {
"name" : "F3",
"persist" : false,
"value" : 0,
"weight" : 0
}, {
"name" : "F4",
"persist" : false,
"value" : 0,
"weight" : 0
}
],
"value" : 0,
"weight" : 0
}, {
"name" : "STI059DETAIL",
"persist" : false,
"nodes" : [{
"name" : "F1",
"persist" : false,
"value" : null,
"weight" : 0
}, {
"name" : "F2",
"persist" : false,
"value" : null,
"weight" : 0
}, {
"name" : "F3",
"persist" : false,
"value" : 0,
"weight" : 0
}, {
"name" : "F4",
"persist" : false,
"value" : 0,
"weight" : 0
}
],
"value" : 0,
"weight" : 0
}
],
"value" : 0,
"weight" : 4
}
],
"weight" : 7
};
我的任务是从weight
为0
且nodes
属性存在的地方删除其中的所有节点。
由于它是一棵树,我试图使用这样的递归函数:
function clean(index, owner){
var node = owner[index],
weight = node.weight;
delete node.weight;
if(typeof node.persist != 'undefined'){
delete node.persist;
}
if(!node.nodes)return;
if(!weight){
owner.splice(index, 1);
}
for(var i = 0; i < node.nodes.length; i++){
clean(i, node.nodes);
}
}
for(var i = 0; i < nodeset.nodes.length; i++){
clean(i, nodeset.nodes);
}
但splice()
不知何故,并没有从那里删除任何东西。我已将其替换为delete owner[index]
,这会在这些节点的位置导致null
值(我不希望看到:(
)。
我的问题:为什么splice()
功能无法正常工作(不删除节点)?另外,我采取了正确的方法吗?如果不是,那么任何其他建议都将受到赞赏。
问候。
测试小提琴HERE,如果它可能有所帮助。
答案 0 :(得分:3)
您可以使用Array.filter
和Array.forEach
过滤掉0权重的节点。首先,定义一个谓词:
var keepNode = function(node) {
return !(node.weight == 0 && node.hasOwnProperty("nodes"));
};
然后是一个清除不想要的孩子的节点然后递归剩下的孩子的函数:
function clean(tree) {
if (tree.nodes) {
tree.nodes = tree.nodes.filter(keepNode);
tree.nodes.forEach(clean);
}
}
最后,处理整个数据结构:
clean(nodeset);
几乎可以肯定的是,在过滤时会有一种更优雅的方式,但是这应该可以完成。
编辑(因为我没注意到IE8标签)
对于IE8(不支持filter
),您有几个选择。您可以使用将大多数EcmaScript函数添加到旧版JS引擎的ES5 shim package。或者,您可以使用此填充程序(可在上面链接的filter
的文档页面中进一步提供):
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp*/)
{
"use strict";
if (this == null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun != "function")
throw new TypeError();
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}
return res;
};
}