ts 中json数组分组

arr: any = [{NAME: "SSS", AGE:"xxxx"},
  {NAME: "AAA", AGE:"xxxx"},
  {NAME: "SSS", AGE:"xxxx"},
  {NAME: "CCC", AGE:"xxxx"},
  {NAME: "SSS", AGE:"xxxx"},
  {NAME: "DDD", AGE:"xxxx"},
  {NAME: "SSS", AGE:"xxxx"},];
FenZu(arr: any) {
var modal = {}, result = [];
   for(var i = 0; i < arr.length; i++) {
       var arrdata = arr[i];
       if (!modal[arrdata.NAME]) {
         result.push({
             NAME: arrdata.NAME,
             data: [arrdata]
        });
        modal[arrdata.NAME] = arrdata;
      } else {
        for (var j = 0; j < result.length; j++) {
           var resultdata = result[j];
           if (resultdata.NAME == arrdata.NAME) {
                resultdata.data.push(arrdata);
                break;
          }
       }
      }
   }
   return result;
}

你可能感兴趣的:(ts 中json数组分组)