使用RXJS运算符

时间:2018-05-22 10:52:38

标签: javascript rxjs rxjs6

我试图用rxjs构建n级树视图。我用普通的javascript实现了同样的目的。找到js代码here



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);




有人可以在这上面显示一些灯光。

1 个答案:

答案 0 :(得分:0)

以下是我可以从https://jvilk.com/MakeTypes/生成的内容。您可能希望稍微清理它,例如使Data和Data1使用相同的类使isNode可选。当你需要做很多这样的事情时,转换器可以派上用场。

    Also you may want to make these as class instead of interface if what you want is class.

   export interface Weather {
  id: number;
  name: string;
  parentDepartmentId?: null;
  items?: (ItemsEntity | null)[] | null;
}
export interface ItemsEntity {
  id: number;
  name: string;
  parentDepartmentId: number;
  items?: (ItemsEntity1 | null)[] | null;
}
export interface ItemsEntity1 {
  id: number;
  name: string;
  parentDepartmentId: number;
  items?: (null)[] | null;
}