我在nodejs应用程序中使用以下代码,从构成邻接列表的数据库行数组构建树:
// Lay out every node in the tree in one flat array.
var flatTree = [];
_.each(rows, function(row) {
flatTree.push(row);
});
// For each node, find its parent and add it to that parent's children.
_.each(rows, function(row) {
// var parent = _.find(flatTree, function(p) {
// p.Id == row.ParentId;
// });
var parent;
for (var i = 0; i < flatTree.length; i++){
if (flatTree[i].Id == row.ParentId) {
parent = flatTree[i];
break;
}
};
if (parent){
if (!parent.subItems) {
parent.subItems = [];
};
parent.subItems.push(row);
}
});
我希望注释掉的_.find
调用与它下面的解决方法for
循环完全相同,但是_.find
永远不会在{{1}中找到父节点} {} flatTree
循环始终如此。
同样,对for
的调用也不起作用,而替代循环也是如此:
_.filter
我使用的是// var rootItems = _.filter(flatTree, function (node) {
// //node.ParentId === null;
// node.NoParent === 1;
// })
var rootItems = [];
for (var i = 0; i < flatTree.length; i++){
if (flatTree[i].ParentId == null){
rootItems.push(flatTree[i]);
}
}
软件包,但已尝试使用常规underscore-node
软件包获得相同的结果。
答案 0 :(得分:3)
错过了library(car)
# tests if the coefficient of Sepal.Width = Petal.Length
linearHypothesis(model, "Sepal.Width = Petal.Length")
Linear hypothesis test
Hypothesis:
Sepal.Width - Petal.Length = 0
Model 1: restricted model
Model 2: Sepal.Length ~ Sepal.Width + Petal.Length
Res.Df RSS Df Sum of Sq F Pr(>F)
1 148 16.744
2 147 16.329 1 0.4157 3.7423 0.05497 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# Other examples:
# tests if the coefficient of Sepal.Width = 0
linearHypothesis(model, "Sepal.Width = 0")
# tests if the coefficient of Sepal.Width = 0.5
linearHypothesis(model, "Sepal.Width = 0.5")
# tests if both are equal to zero
linearHypothesis(model, c("Sepal.Width = 0", "Petal.Length = 0"))
。
return
在您的代码中,不会返回任何内容,因此默认情况下会返回var parent = _.find(flatTree, function(p) {
return p.Id == row.ParentId; // Return true if the ID matches
^^^^^^ <-- This
});
,undefined
将不包含任何数据。