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

tjvv9vkg  于 2023-04-10  发布在  Java
关注(0)|答案(3)|浏览(126)

我有一个API,它返回简单的json。这需要转换成嵌套的JSON数组在父/子关系的基础上"ParentId"在每个节点。下面是示例JSON数组,我从API ..可以eanyone张贴任何函数的JavaScript实现这一点的例子?
我正在尝试在react中创建剑道树列表视图。
我想要这个

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

        }]
      
    }]
  },

];

出口违约员工;

fiei3ece

fiei3ece1#

这是解决方案。

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

说明:

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

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

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

wydwbb8l2#

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

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
         
                                         }
                                        ]
         
                         }]
         
         
          }
        ]
    }
   ];
vsaztqbk

vsaztqbk3#

// 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))
    }
}

相关问题