码迷,mamicode.com
首页 > Web开发 > 详细

json 按照字段分类

时间:2017-12-13 14:26:09      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:end   ==   cat   post   length   for   style   let   pre   

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

 

json 按照字段分类

标签:end   ==   cat   post   length   for   style   let   pre   

原文地址:http://www.cnblogs.com/cynthia-wuqian/p/8023547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!