anders
anders

Reputation: 1545

Insert jsonobject into new array

Say that you have the following jsonObject

      var arrayWithValuesAndGroups = [{
            "TestObject": "Object1",
            "GraphGroup": {
                "Test": {
                    "Group": "A",
                    "Value": "6"
                },
                "Test2": {
                    "Group": "B",
                    "Value": "5"
                }
            }
        },

        {
            "TestObject": "Object2",
            "GraphGroup": {
                "Test": {
                    "Group": "A",
                    "Value": "9"
                },
                "Test2": {
                    "Group": "B",
                    "Value": "12"
                }
            }
        },

        {
            "TestObject": "Object3",
            "GraphGroup": {
                "Test": {
                    "Group": "A",
                    "Value": "99"
                },
                "Test2": {
                    "Group": "B",
                    "Value": "16"
                }
            }
        }
    ]

I want to create a new object with all groups and all values that have that group should be in that array. For example I want the above object to be converted into the bellow

     {
        "A": {
            "Test1": {
                "0": "6",
                "1": "9",
                "2": "99"
            }
        },
        "B": {
            "Test2": {
                "0": "5",
                "1": "12",
                "2": "16"
            }
        }
    }

What strategy would you use?

Upvotes: 0

Views: 54

Answers (3)

Manivannan
Manivannan

Reputation: 3254

May be following code can help u to solve this, fiddle http://jsfiddle.net/jesamzjv/

    function GetMyFormat(arrayWithValuesAndGroups){

        var finalOb = {};

        pushToOb = function(group, value, test){
            if(!finalOb[group]){
              finalOb[group] = {};
              finalOb[group][test] = {};
            }  
              var myOb = finalOb[group][test];
              var count = Object.keys(myOb).length;
                myOb[count] = value;
        }

        addToAnAr = function(ob){
            for (var i in ob){
              pushToOb(ob[i].Group,ob[i].Value,i)
            }
        }

        for(var i in arrayWithValuesAndGroups){
          item = arrayWithValuesAndGroups[i];
          addToAnAr( item["GraphGroup"] );
        }
        return finalOb;
    }
    console.log(GetMyFormat(arrayWithValuesAndGroups))

Upvotes: 0

easywaru
easywaru

Reputation: 1153

Try this.

Concept of Object and Array is very important on js and another code.

Practice is only way.

var newObject = {};

 for(var i=0,iLen=arrayWithValuesAndGroups.length;i<iLen;i++){
     var TestGroupObject = arrayWithValuesAndGroups[i];
     console.log(TestGroupObject);

     // {
     //    "TestObject": "Object1",
     //    "GraphGroup": {
     //        "Test": {
     //            "Group": "A",
     //            "Value": "6"
     //        },
     //        "Test2": {
     //            "Group": "B",
     //            "Value": "5"
     //        }
     //     }
     // }

     var GraphGroupObject = TestGroupObject.GraphGroup;
     console.log(GraphGroupObject);
     // {
     //        "Test": {
     //            "Group": "A",
     //            "Value": "6"
     //        },
     //        "Test2": {
     //            "Group": "B",
     //            "Value": "5"
     //        }
     //     }

     var GraphGroupObjectKeys=Object.keys(GraphGroupObject);
     for(var j=0,jLen=GraphGroupObjectKeys.length;j<jLen;j++){
         var GraphGroupObjectKey = GraphGroupObjectKeys[j];
         console.log(GraphGroupObjectKey)
         // keys are Test, Test2

         // GraphGroupObject[GraphGroupObjectKey]
         // {
         //     "Group": "A",
         //     "Value": "6"
         // }

         var Group = GraphGroupObject[GraphGroupObjectKey].Group;
         var Value = GraphGroupObject[GraphGroupObjectKey].Value;

         if(!newObject[Group]){
             newObject[Group]={};
         }
         if(!newObject[Group][GraphGroupObjectKey]){
             newObject[Group][GraphGroupObjectKey]={};
         }
         newObject[Group][GraphGroupObjectKey][i] = Value;
     }
 }

Upvotes: 0

setec
setec

Reputation: 16100

You need to transform one data structure to another.

This is typically done by creation of new object and setting its values from original object within a series of transformations (which in this case are iterations, array creations, value assignments).

While it can be easily done with vanilla js, you can also use lodash library which greatly facilitates such transformations by giving methods to iterate, access keys, values and so on.

I'd not give you an exact solution for your specific data objects just because 1) you've asked about strategy 2) SO is't a place to ask others do your work 3) an answer should be useful to other persons with other data structures.

Upvotes: 1

Related Questions