Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将jsonobject插入新数组_Javascript_Json - Fatal编程技术网

Javascript 将jsonobject插入新数组

Javascript 将jsonobject插入新数组,javascript,json,Javascript,Json,假设您有以下jsonObject var arrayWithValuesAndGroups = [{ "TestObject": "Object1", "GraphGroup": { "Test": { "Group": "A", "Value": "6" },

假设您有以下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"
                }
            }
        }
    ]
我想创建一个包含所有组的新对象,所有包含该组的值都应该在该数组中。例如,我希望将上面的对象转换为下面的

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

你会使用什么策略

您需要将一种数据结构转换为另一种数据结构

这通常是通过创建新对象并在一系列转换(在本例中是迭代、数组创建和值分配)中从原始对象设置其值来完成的

虽然使用vanilla js可以很容易地完成,但您也可以使用lodash库,它通过提供迭代、访问键、值等方法极大地促进了此类转换

我不会为您的特定数据对象提供精确的解决方案,因为1)您已经询问了策略2)因此这不是一个让其他人做您的工作的地方3)答案应该对具有其他数据结构的其他人有用。

试试这个

对象和数组的概念在js和其他代码中非常重要

练习是唯一的方法

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;
     }
 }
var newObject={};

对于(var i=0,iLen=arrayWithValuesAndGroups.length;i可能是以下代码可以帮助您解决此问题,fiddle


非常感谢你的帮助,完美地解决了我的问题。
    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))