组装类树形数据

data为后台返回的数据,根据条件进行层级归类重组数据结构

// 第一层样品
    regroup(data) {
      let newData = [];
      for (var i = 0; i < data.length; i++) {
        if (data[i].isGroup == 1) {
          newData.push(data[i]);
        }
      }
      return newData;
    },
    // 第二层子样
    dast(data) {
      let newData2 = [];
      for (var i = 0; i < data.length; i++) {
        if (data[i].isGroup == 0 && data[i].havePxy == "是") {
          newData2.push(data[i]);
        }
      }
      return newData2;
    },
    // 第三层平行样
    thirdPx(data) {
      let newData3 = [];
      for (var i = 0; i < data.length; i++) {
        if (data[i].isGroup == 0 && data[i].havePxy == "否") {
          newData3.push(data[i]);
        }
      }
      return newData3;
    },
    // 组合数据
    concantData(data) {
      let lastData = [];
      this.regroup(data).map(item => {
        let zy = [];
        this.dast(data).map(tag => {
          if (item.id == tag.fkZyid) {
            zy.push(tag);
          }
        });
        lastData.push({
          yp: item,
          zyList: zy
        });
      });
      lastData.map(itms => {
        itms.zyList.forEach((it, i) => {
          let pxy = [];
          this.thirdPx(data).map(itp => {
            if (it.id == itp.fkZyid) {
              pxy.push(itp);
            }
          });
           it["pxyList"] = pxy;
        });
      });
      console.log(lastData, 456);
      return lastData;
    },

你可能感兴趣的:(javascript)