JSFiddle - React, Tailwind, and code Playground
JavaScript
Array.prototype.groupBy = function(keyName) {
var res = {};
this.forEach(function(x) {
var k = x[keyName];
if(!k){
recursiveFind(x, keyName, x, 0);
}else {
var v = res[k];
if (!v) v = res[k] = [];
v.push(x);
}
})
function recursiveFind(val, keyName, orig, max) {
if (val && max < 3) {
if ( val instanceof Array) {
val.forEach(function(x) { recursiveFind(x, keyName, orig, max+1)});
} else if ( val.toString() == "[object Object]") {
console.log("res" + res);
var k2 = val[keyName];
if (!k2) {
for(var i in val) {
recursiveFind(val[i], keyName, orig, max+1);
}
} else {
if(!res[k2]) {
res[k2] = [];
}
res[k2].push(orig);
}
}
}
}
return res;
};
var myObject = {
"Category":{
"old_modi":1,
"new_modi":1,
"FORM":[
{
"name":"form1",
"date":"08/08/2012",
"location":"abc",
"PERSON":[
{
"ID":1,
"author":"xyz"
}
]
},
{
"name":"form2",
"date":"08/08/2012",
"location":"abc",
"PERSON":[
{
"ID":2,
"author":"kk"
}
]
},
{
"name":"form3",
"date":"08/08/2012",
"location":"abc",
"PERSON":[
{
"ID":1,
"author":"xyz"
}
]
}
]
}
}
var byJob...