我正在尝试使用 rxjs 构建 n 级树视图。我用普通的javascript实现了同样的效果。在这里找到js代码
var x = [{"id":1,"name":"Admin","parentDepartmentId":null},{"id":2,"name":"Development","parentDepartmentId":1},{"id":3,"name":"Research and Development","parentDepartmentId":1},{"id":4,"name":"FE","parentDepartmentId":2},{"id":5,"name":"BE","parentDepartmentId":2},{"id":6,"name":"Testing","parentDepartmentId":1},{"id":7,"name":"Unit Test","parentDepartmentId":6},{"id":8,"name":"Integration Test","parentDepartmentId":6},{"id":9,"name":"HR","parentDepartmentId":null}];
function getRecords(parents) {
parents.forEach((parent) => {
var result = getChildData(parent);
parent.items = result;
if (result.length > 0) {
getRecords(result);
}
})
return parents;
}
function getChildData(parent) {
return x.filter(record => record.parentDepartmentId == parent.id)
}
function getParents(x){
return x.filter(record => record.parentDepartmentId == null);
}
var parents = getParents(x)
var result = getRecords(parents);
console.log(result);
有人可以对此进行一些说明。