javascriptexpressmongoosetransloadit

Parsing an array of objects and pushing selectively to new object


I am trying to parse a JSON response from transloadit and save off the result objects in groups to store separately.

This is the JSON: https://jsonblob.com/552917cee4b0237a964c0de1

I am trying this...

var results = response.results;

         var versions = {};

         for (var index in results) {
            var this_key = index;
            for (var i = 0; i < results[index].length; i++) {
               if(results[index][i].md5hash === media.md5){
                  versions[this_key] += results[index][i];
               }
            }
         }

But when I console versions I am only getting

{":original":"undefined[object Object]"}

Whereas I would have expected something closer to what I want to achieve which is

{":original":[object Object],"l":[object Object]}

The intention is to insert this into MongoDB so that I am left with

"versions":{"l":{"name":"foo"...},"m":{"name":"bar"...}}

Solution

  • The issue you are facing is because of the versions[this_key] not initialized.

    Check http://jsfiddle.net/Lhzgc7tq/

    var results = response.results;
    var versions = {};
    for (var index in results) {
        var this_key = index;
        for (var i = 0; i < results[index].length; i++) {
            if(results[index][i].md5hash === media.md5){
                versions[this_key] = "";
                versions[this_key] += results[index][i];
            }
        }
    }
    
    console.log(versions);