简体   繁体   English

Javascript数组json转换为父子json对象

[英]Javascript array json converting to parent child json object

 var rawData1 = {
  root: [
    "children": [
      {
        "country": "Japan",
        "children": [
          {
            "C1": "Japan 225343-17510-ADV HIV"
          },
          {
            "C2": "Japan 245123-142567"
          }
        ]
      },
      {
        "country": "EU",
        "children": [
          {
            "C1": "345112-765431-HID
          },
          {
            "C2": "22535"
          },
          {
            "C3": "EU 22535"
          },
          {
            "C4": "EU 8988-90"
          },
          {
            "C5": "EU 8988-90"
          }
        ]
      }, {
        "country": "Canada",
        "children": [
          {
            "C1": "Canada 345112-765431-HID"
          }
        ]
      }, {
        "country": "Switzerland",
        "children": [
          {
            "C1": "Switzerland 345112-765431-HID"
          }, {
            "C2": "Switzerland 22535"
          }, {
            "C3": "Switzerland 8988-90"
          }, {
            "C4": "Switzerland 8988-90"
          }
        ]
      }, {
        "country": "US",
        "children": [
          {
            "C1": "US 345112-765431-HID",
          }, {
            "C2": "US 22535"
          }, {
            "C3": "US 46489"
          }
        ]
      }
    ]
  ]
};

I want to convert the rawData array to rawData1 array.我想将 rawData 数组转换为 rawData1 数组。

rawData1 is the parent-child in form of Json. rawData1 是 Json 形式的父子。

The parent and Child are decided based on key only parent 和 child 仅根据 key 决定

( NOt key-value only key Eg: if key is country then it is parent or else it is child) if key is C1 or C2 etc (不是仅键值键,例如:如果键是国家,则它是父项,否则它是子项)如果键是 C1 或 C2 等

then it is child array with each separate object of key-value only you can see the code for more detail那么它是每个单独的键值对象的子数组,只有您可以查看代码以获取更多详细信息


Thank you ver much you code worked very well非常感谢你的代码工作得很好

Now i want one more Child like in现在我想要一个像这样的孩子

{ {

root:{根:{

“country” : "Japan" “国家”:“日本”

children:[孩子们:[

            {"C1":"879"}

            {"C2":"jk"}

            children:[

                            {"C3":"Code","C4":"Codei"}, //for C3 and C4 create nested children to above children

                            {"C5":"Code","C6":"Codei"} // if data is present C5 andC6



                            ]

] ]

} }

This is want i want your variable call childrenArray inside this i want one more array called ChildrenSubArray这是我想要你的变量调用 childrenArray 在这个里面我想要一个名为 ChildrenSubArray 的数组

Here's a simple solution using a for in loop within a map function:这是在 map 函数中使用 for in 循环的简单解决方案:

 var rawData = { results: [{ "country": "Japan", "C1": "Japan 225343-17510-ADV HIV", "C2": "Japan 245123-142567" }, { "country": "EU", "C1": "345112-765431-HID", "C2": "22535", "C3": "EU 22535", "C4": "EU 8988-90", "C5": "EU 8988-90" }, { "country": "Canada", "C1": "Canada 345112-765431-HID", }, { "country": "Switzerland", "C1": "Switzerland 345112-765431-HID", "C2": "Switzerland 22535", "C3": "Switzerland 8988-90", "C4": "Switzerland 8988-90" }, { "country": "US", "C1": "US 345112-765431-HID", "C2": "US 22535", "C3": "US 46489" }] }; var results = rawData.results; children = results.map(function(x) { var childrenArray = []; for(const child in x) { if(child != "country") { childrenArray.push({ [child]: x[child] }) } } return { "country": x.country, "children": childrenArray } }) var rawData1 = { root: [{ "children": children }] } console.log(JSON.stringify(rawData1, null, 4));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM