Any standard JavaScript library for transforming JS objects -


i have complex nested javascript object me. requirement flatten out object or of members in it.

var obj = {      a: {         b: {           c: {             d: "varun"              },           e: "kumar"         }     }    }; 

the expected resultant object:

{d: "varun", e: "kumar"}  

i have written simple transform utility accept accessor map in form {"a.b.c.d": "d"} , transform object new object. not supporting arrays now. also, transform utility can reduce complex object simpler 1 , not vice-versa (ie. construct new member object simple member).

"use strict"; var objectutil = (function () {     // constructor     var cls = function () {      };      // public static     cls.getvaluefromaccessor = function (obj, accessor) {         if (obj == null || accessor == null)             return null;          return accessor.split(".").reduce(function(prev, current, index) {             var reducedobject = prev;             if (index == 1)                 reducedobject = obj[prev];              if (reducedobject == null)                 return null;              return reducedobject[current];         });     };      cls.transform = function(obj, accessormap, overlay) {         var result;          if (overlay)             result = obj;         else             result = {};          (var k in accessormap) {             result[accessormap[k]] = cls.getvaluefromaccessor(obj, k);         }          return result;     };      return cls; })();  var obj = {      a: {         b: {           c: {             d: "varun"              },           e: "kumar"         }     }    };  var accessormap = {     "a.b.c.d": "d",     "a.b.e": "e" }  objectutil.getvaluefromaccessor(obj, "a.b.c.d"); console.log(objectutil.transform(obj, accessormap, false)); console.log(objectutil.transform(obj, accessormap, true)); 

is there standard way of transforming objects 1 form another. libraries available this?

var converter = new (function(){     var divider = ".";     var doflatten = function(source, path, newobj){         for(var in source){             if(source.hasownproperty(i)){                 var temppath = (path == "") ? : path + divider + i;                 if(typeof source[i] == "object"){                     doflatten(source[i], temppath, newobj);                 }                 else{                     newobj[temppath] = source[i];                 }             }         }     };     this.flatten = function(source){         if(typeof source == "object"){             var newobj = {};             doflatten(source, "", newobj);             return newobj;         }         return source;     };     this.expand = function(source){         var dest = {};         if(typeof source == "object"){             for(var in source){                 if(source.hasownproperty(i)){                     var path = i.split(divider);                     var temp = dest;                     var prevtemp = dest;                     for(var j in path){                         if(!temp.hasownproperty(path[j])){                             temp[path[j]] = {};                         }                         prevtemp = temp;                         temp = temp[path[j]];                     }                     prevtemp[path[path.length - 1]] = source[i];                 }             }             return dest;         }         return source;     }; }); 

usage:

var flatobj = converter.flatten({     a: {         b: {             c: {                 d: "varun"                },             e: "kumar"         }     }    }); console.log(flatobj); // {"a.b.c.d": "varun", "a.b.e": "kumar"}  var expanded = converter.expand(flatobj); console.log(expanded); // { //    a: { //        b: { //            c: { //                d: "varun"    //            }, //            e: "kumar" //        } //    }    // } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -