如何将简单的JSON置于JavaScript中的JSON嵌套

发布于 2025-02-11 07:08:08 字数 846 浏览 2 评论 0原文

我有一个返回简单JSON的API。需要根据每个节点中的“ parentId”将哪个在父/子关系中转换为嵌套的JSON数组。below是我从API中获得的示例JSON数组。.enyone可以在JavaScript中发布任何函数示例以实现这一目标吗?

我正在尝试在React中使Kendo 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

        }]
      
    }]
  },

];

出口默认员工;

I have a API which return simple json. Which need to be converted into nested JSON array in a parent/child relation based on "ParentId" in each node.Below is the sample JSON array which I get from API..Can enyone post any example of function in JavaScript to achieve this?

I'm trying to make kendo TreeList view in react.

I want this:

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

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

 }
      
    
];

transform to this:

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

        }]
      
    }]
  },

];

export default employees;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千笙结 2025-02-18 07:08:08

这是解决方案。

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

说明:

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

我们还可以将ID作为对象的关键

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

Here is the solution.

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

Explanation:

  • We are using reduceRight. I will start reading the array from the last index.
  • In the second step, we are creating an object and adding it back to the second last object, and so on.

We can also make id as a key of object

const result = employees.reduceRight(
    (all, item) => ({ [item.id]: [{ ...item, ...all }] }),
    {}
);
堇年纸鸢 2025-02-18 07:08:08

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

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

I tried those solutions but they dont work for me if i have response like this:

 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
    },
   ]

this is how i want response to look like:

 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
         
                                         }
                                        ]
         
                         }]
         
         
          }
        ]
    }
   ]; 
深府石板幽径 2025-02-18 07:08:08
// 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))
    }
}
// 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))
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文