如何在 JavaScript 中将简单的 json 转换为嵌套的 json

问题描述 投票:0回答:3

我有一个返回简单 json 的 API。哪些需要根据每个节点中的“ParentId”转换为父/子关系中的嵌套 JSON 数组。下面是我从 API 获得的示例 JSON 数组。任何人都可以在 JavaScript 中发布任何函数示例来实现这一点吗?

我正在尝试在反应中制作剑道 TreeList 视图。

我想要这个:

   const employees = [
  {  
    "id" :1,
    "IdChild": 23890952,
    "ParentId": null
  },
  {
      "id" :2,
      "IdChild": 23890953,
      "ParentId": 23890952

},
{
   "id":3,
   "IdChild": 23890954,
    "ParentId": 23890953

 }
      
    
];

转换为:

const employees = [
  {  
    "id" :1,
    "IdChild": 23890952,
    "ParentId": null,
   
    "employees":[{
      "id" :2,
        "IdChild": 23890953,
        "ParentId": 23890952,
        
        "employees":[{
            "id":3,
            "IdChild": 23890954,
            "ParentId": 23890953

        }]
      
    }]
  },

];

导出默认员工;

javascript nested parent-child parent
3个回答
1
投票

这里是解决方案。

const result = employees.reduceRight(
    (all, item) => ({ ["employees"]: [{ ...item, ...all }] }),
    {}
);

说明:

  • 我们正在使用 reduceRight。我将从最后一个索引开始读取数组。
  • 在第二步中,我们正在创建一个对象并将其添加回倒数第二个对象,依此类推。

我们也可以把id作为object的key

const result = employees.reduceRight(
    (all, item) => ({ [item.id]: [{ ...item, ...all }] }),
    {}
);

0
投票

我尝试了这些解决方案,但如果我有这样的回应,它们对我不起作用:

 const employees =[
    {
       
        "IdChild": 23982608,
        "ParentId": null,
        "id" :1
    },
    {
       
        "IdChild": 23982609,
        "ParentId": 23982608,
         "id" :2
    },
    {
        
        "IdChild": 23982610,
        "ParentId": 23982608,
         "id" :3
    },
    {
      
        "IdChild": 23982611,
        "ParentId": 23982608,
         "id" :4
    },
    {
      
        "IdChild": 23982978,
        "ParentId": 23982611,
         "id" :5
    },
    {
       
        "IdChild": 23982979,
        "ParentId": 23982978,
         "id" :6
    },
   ]

这就是我想要的响应方式:

 const employees =[
    

    {
       
        "IdChild": 23982608,
        "ParentId": null,
        "id" :1,
        "employees":[
           {
       
            "IdChild": 23982609,
            "ParentId": 23982608,
            "id" :2
          },
          {
        
            "IdChild": 23982610,
            "ParentId": 23982608,
            "id" :3
          },
          {
      
            "IdChild": 23982611,
            "ParentId": 23982608,
            "id" :4,
            "employees":
                        [
                          {
                          "IdChild": 23982978,
                          "ParentId": 23982611,
                          "id" :5,
            
                          "employees": [
                                        {
         
                                          "IdChild": 23982979,
                                          "ParentId": 23982978,
                                          "id" :6
         
                                         }
                                        ]
         
                         }]
         
         
          }
        ]
    }
   ]; 

0
投票
// Takes in an array of objects with parent and child keys 
// and returns an array with one nested object
function objectify(objs){
    // loop through all the elements to create an empty 
    // array corresponding to the key "employees"
    objs.forEach(obj => obj["employees"] = []);
    retVal = []
    // Assumes the first element is always the root/parent
    const root = objs.shift()
    retVal.push(root)
    objs.forEach(obj => insert(root, obj));
    return retVal

    // Helper function to insert every object to the correct parent
    function insert(parent, curr){
        if(parent.IdChild === curr.ParentId) {parent["employees"].push(curr); return}
        parent["employees"].forEach((child)=>insert(child, curr))
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.