javascript - split and create an array from a json object in js -
i have given exif of image split each key of json object , create sorted array it. output i'm expecting
{ "app14":[ {"app14flags0": 16384}, {"app14flags1": 0}, {"colortransform": 1} ], "composite":[ {"aperture": 2.8} {"circleofconfusion": 0.0309526315549036} {"datetimecreated": "2013:08:04 13:15:03+00:00"} {"focallength35efl": 97.0717484087605} {"..."} ], "sourcefile": "c.jpg", "xmp":[ {"alreadyapplied": true}, {"approximatefocusdistance": 1.17}, {"autolateralca": 0}, {"blacks2012": 0} ] }
my last console.log({key:values}); giving me same array. js fiddle
var the_keys =[]; _.foreach(data, function(n, key) { //creating array of uniq keys if(key.indexof(':')> -1) { if (_.includes(the_keys, key.split(':')[0] ) == false) { the_keys.push(key.split(':')[0]); } } else { the_keys.push(key); } }); console.log(the_keys); _.foreach(the_keys, function(key) { var values = [] _.foreach(data, function(k, n) { if ((_.includes(the_keys, key.split(':')[0] ) ) || k == key) { values.push(k); } }); console.log({key:values}); });
i'm splitting each keys cause create html table exif viewer site
https://jsfiddle.net/l9a9h5m3/3/
give look. splitting key , building object piece piece. last token assigning value
_.foreach(data, function(v, k){ kparts = k.split(':'); var lastobj = returnobj; for(var i=0; i<kparts.length - 1; i++){ var kpart = kparts[i] if(!lastobj.hasownproperty(kpart)) lastobj[kpart] = {} lastobj = lastobj[kpart]; } lastobj[kparts[kparts.length - 1]] = v; });
Comments
Post a Comment