json 按照字段分类

      let arr = [
        {
          Category:'A',
          Amount:1,
        },{
          Category:'B',
          Amount:2,
        },{
          Category:'A',
          Amount:3,
        },{
          Category:'C',
          Amount:4,
        },{
          Category:'B',
          Amount:5,
        },{
          Category:'C',
          Amount:6,
        },{
          Category:'A',
          Amount:7,
        },{
          Category:'C',
          Amount:8,
        }
      ];

      let temp = {};
      let end = [];

      for(let i = 0; i < arr.length; i++){

        let a = arr[i];

        if(!temp[a.Category]){

          end.push({
            Category: a.Category,
            data: [a]
          });
          temp[a.Category] = a;

        }else{

          for(let j = 0; j < end.length; j++){

            let b = end[j];

            if(b.Category == a.Category){
              b.data.push(a);
              break;
            }
          }

        }
      };

      console.log(end);

 

posted @ 2017-12-13 11:24  Cynthia娆墨旧染  阅读(939)  评论(0编辑  收藏  举报