示例:http: //jsfiddle.net/yeehawjared/bawv0790/
我正在构建一个打开网页的应用程序,加载大型数据树结构的 JSON。TreeModel.js 很好地解析了这个,一切都很好。
随着时间的推移,浏览器会以较小的数据树的形式接收更新。我正试图将我的头结合additionalData
到masterTree
. 我想不出一种同时走两个节点并进行逐节点比较的方法。node.model.x
如果可以的话,聚合属性并添加子项(如果它们不存在)会很容易。
在下面的代码中,我浏览了附加数据 - 但我不知道如何有效地将新节点组合到masterTree
. 有人可以用伪代码帮助我的方法或为我指明正确的方向吗?不断更新我的最佳方式是什么masterTree
?
非常感谢。
var tree = new TreeModel();
var masterTree = tree.parse(data1);
var additionalData = tree.parse(data2);
additionalData.walk(function (node) {
// compare additionalData to the masterTree
if (node.model.id == masterTree.model.id) {
console.debug('match, combine the attributes')
} else {
// add the additional node to the materTree
}
});