0

I have a given exif of an image I would like to split each key of a json object and create a sorted array with it. The output I'm expecting is

{
  "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}); is giving me back the same array. js fiddle

var the_keys =[];
_.forEach(data, function(n, key) {
  //creating an 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 I would like to create an HTML table like for this exif viewer site

2
  • Hi Tushar! from my gist my expected output is like I described in the first part of my questions { "APP14":[ {"APP14Flags0": 16384}, {"APP14Flags1": 0}, {"ColorTransform": 1} ], Commented Jul 2, 2015 at 6:45
  • For the desired output ... the new javascript object is enough, no need to create the table gist Commented Jul 2, 2015 at 7:00

1 Answer 1

1

https://jsfiddle.net/L9a9h5m3/3/

Give this a look. Its basically splitting up the key and then building the object piece by piece. For the last token its assigning the 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;

});

https://jsfiddle.net/L9a9h5m3/3/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.