nyxem1 nyxem1 - 7 months ago 8
Javascript Question

Reducing a JSON data

I have a data source pumping out data in the form:

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
}]


As per my requirement, I have to push the data to an array with the following format:

[{
"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]
}
}],
}
}]


I would like to categorize the data in the give format above. Basically it will create two categories,
Engineering
and
mbbs
.
I would appreciate any guidance towards this. My solution works but in case of a number of records, i can't figure out why all my data is not iterated.
I am using
forEach
loop.

Answer

A proposal with a single loop and a temporary object.

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

Comments