JavaScript Flatter Array Object
by rajeshpillai
JavaScript
var myarr = [ ];
myarr[0] =[{"name":"john","age":"50","location":"san diego"}
,{"name":"jane","age":"25","location":"new york"}
,{"name":"susane","age":"10","location":"los angeles"}
];
myarr[1] =[{"smoker":"yes","drinker":"no","insured":"no"}
,{"smoker":"no","drinker":"no","insured":"yes"}
,{"smoker":"no","drinker":"yes","insured":"no"}
];
myarr[2] =[{"status":"married","children":"none"}
,{"status":"unmarried","children":"one"}
,{"status":"unmarried","children":"two"}
];
function merge(a, b) {
a = a || { };
for(var k in b)
if(b.hasOwnProperty(k))
a[k] = b[k];
return a;
}
var flat = [ ];
for(var i = 0; i < myarr.length; ++i)
for(var j = 0; j < myarr[i].length; ++j)
flat[j] = merge(flat[j], myarr[i][j]);
flat.sort(function(a, b) {
a = a.location;
b = b.location;
if(a < b)
return -1;
if(a > b)
return 1;
return 0;
});
console.log(flat);