TS列表分组


  groupArr(list: Array<any>, field: string, fieldName: string): Array<any> {
    var fieldList: Array<any> = [],
      att = [];
    list.forEach((e) => {
      if (!fieldList.some((m: any) => m.id == e[field])) {
        fieldList.push({ id: e[field], name: e[fieldName] });
      }
    });

    for (let item of fieldList) {
      var arr = list.filter((e) => {
        return e[field] == item.id;
      });
      att.push({
        GroupId: item.id,
        GroupName: item.name,
        list: arr,
      });
    }
    console.log(att);
    return att;
  }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM