减少JSON数据

时间:2016-05-02 12:14:36

标签: javascript jquery arrays json

我有一个数据源以表格形式输出数据:

arr = [{
"type": "engineering",
"name": "Physics",
"name_id": 6,
"strength": [21,33,12],
"batch": 191
}, {
"type": "engineering",
"name": "Computer and Mathematics",
"name_id": 8,
"strength": [1,3],
"batch": 192
}, {
"type": "engineering",
"name": "Computer and Mathematics",
"name_id": 8,
"strength": [1,33],
"batch": 193
}, {
"type": "engineering",
"name": "Physics",
"name_id": 6,
"strength": [2,33],
"batch": 194
}, {
"type": "mbbs",
"name": "Surgery",
"name_id": 16,
"strength": [21,3],
"batch": 195
}, {
"type": "mbbs",
"name": "Dental",
"name_id": 26,
"strength": [21,3],
"batch": 196
 }, {
"type": "mbbs",
"name": "Dental",
"name_id": 26,
"strength": [21,33,43],
"batch": 197
}]

根据我的要求,我必须将数据推送到具有以下格式的数组:

[{
"engineering": {
    "subject": [{
        "name":{
            "name": "Physics",
            "name_id": 6
        },
        "class":{
            "batch": [191,194],
            "strength":[21,33,12,2,33]
        }
    },
        {
            "name":{
                "name": "Computer and Mathematics",
                "name_id": 8
            },
            "class":{
                "batch": [192,193],
                "strength":[1,3,1,33]
            }
        }],
},
"mbbs":{
    "subject": [{
        "name":{
            "name": "Surgery",
            "name_id": 16
        },
        "class":{
            "batch": 195,
            "strength":[21,3,195]
        }
    },
        {
            "name":{
                "name": "Dental",
                "name_id": 8
            },
            "class":{
                "batch": [196,197],
                "strength":[21,3,21,33,43]
            }
        }],
     }
  }]

我想以上面的给定格式对数据进行分类。基本上,它会创建两个类别Engineeringmbbs。 我将不胜感激任何指导。我的解决方案有效,但是在有大量记录的情况下,我无法弄清楚为什么我的所有数据都不会被迭代。 我正在使用forEach循环。

2 个答案:

答案 0 :(得分:2)

尝试这种方法

var map = {}; 
arr.forEach(function(val){
  map[ val.type ] = map[ val.type ] || {};
  map[ val.type ][ "subject" ] = map[ val.type ][ "subject" ] || [];
  map[ val.type ][ "subject" ][ val.name_id ] = map[ val.type ][ "subject" ][ val.name_id ] || { name : { name : val.name, name_id : val.name_id }, class : { batch : val.batch, strength : [] } };
  map[ val.type ][ "subject" ][ val.name_id ][ "class" ][ "strength" ] = map[ val.type ][ "subject" ][ val.name_id ][ "class" ][ "strength" ].concat(val.strength);
});

现在这个地图有你想要的聚合,你可以根据你的要求简单地格式化

  var output = Object.keys(map).map(function(val){
    map[val]["subject"] = map[val]["subject"].filter(function(val){
       return val
    });
    return map[ val ];
 })

<强>样本

arr = [{
"type": "engineering",
"name": "Physics",
"name_id": 6,
"strength": [21,33,12],
"batch": 191
}, {
"type": "engineering",
"name": "Computer and Mathematics",
"name_id": 8,
"strength": [1,3],
"batch": 192
}, {
"type": "engineering",
"name": "Computer and Mathematics",
"name_id": 8,
"strength": [1,33],
"batch": 193
}, {
"type": "engineering",
"name": "Physics",
"name_id": 6,
"strength": [2,33],
"batch": 194
}, {
"type": "mbbs",
"name": "Surgery",
"name_id": 16,
"strength": [21,3],
"batch": 195
}, {
"type": "mbbs",
"name": "Dental",
"name_id": 26,
"strength": [21,3],
"batch": 196
 }, {
"type": "mbbs",
"name": "Dental",
"name_id": 26,
"strength": [21,33,43],
"batch": 197
}]

   var map = {}; 
    arr.forEach(function(val){
      map[ val.type ] = map[ val.type ] || {};
      map[ val.type ][ "subject" ] = map[ val.type ][ "subject" ] || [];
      map[ val.type ][ "subject" ][ val.name_id ] = map[ val.type ][ "subject" ][ val.name_id ] || { name : { name : val.name, name_id : val.name_id }, class : { batch : val.batch, strength : [] } };
      map[ val.type ][ "subject" ][ val.name_id ][ "class" ][ "strength" ] = map[ val.type ][ "subject" ][ val.name_id ][ "class" ][ "strength" ].concat(val.strength);
    });


 var output = Object.keys(map).map(function(val){
        map[val]["subject"] = map[val]["subject"].filter(function(val){
           return val
        });
        return map[ val ];
     })
 
document.body.innerHTML +=  JSON.stringify( output, 0, 4 );

答案 1 :(得分:2)

具有单个循环和临时对象的提案。

&#13;
&#13;
var arr = [{ "type": "engineering", "name": "Physics", "name_id": 6, "strength": [21, 33, 12], "batch": 191 }, { "type": "engineering", "name": "Computer and Mathematics", "name_id": 8, "strength": [1, 3], "batch": 192 }, { "type": "engineering", "name": "Computer and Mathematics", "name_id": 8, "strength": [1, 33], "batch": 193 }, { "type": "engineering", "name": "Physics", "name_id": 6, "strength": [2, 33], "batch": 194 }, { "type": "mbbs", "name": "Surgery", "name_id": 16, "strength": [21, 3], "batch": 195 }, { "type": "mbbs", "name": "Dental", "name_id": 26, "strength": [21, 3], "batch": 196 }, { "type": "mbbs", "name": "Dental", "name_id": 26, "strength": [21, 33, 43], "batch": 197 }],
    grouped = [];

arr.forEach(function (a) {
    var o = {};
    if (!this.type[a.type]) {
        o[a.type] = { subject: [] };
        this.type[a.type] = o[a.type].subject;
        grouped.push(o);
    }
    if (!this.name[a.name_id]) {
        this.name[a.name_id] = { "name": { "name": a.name, "name_id": a.name_id }, "class": { "batch": undefined, "strength": [] } };
        this.type[a.type].push(this.name[a.name_id]);
    }
    if (typeof this.name[a.name_id].class.batch === 'number') {
        this.name[a.name_id].class.batch = [this.name[a.name_id].class.batch];
    }
    Array.isArray(this.name[a.name_id].class.batch) && this.name[a.name_id].class.batch.push(a.batch);
    if (this.name[a.name_id].class.batch === undefined) {
        this.name[a.name_id].class.batch = a.batch;
    }
    this.name[a.name_id].class.strength = this.name[a.name_id].class.strength.concat(a.strength);
}, { type: Object.create(null), name: Object.create(null) });

document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');
&#13;
&#13;
&#13;