简体   繁体   English

将数据添加到嵌套的 JSON object 以及基于数组的子项 - Javascript/REACT

[英]Adding data to a nested JSON object with children based on an array - Javascript/REACT

Hello stackoverflow community, I've been creating my own fullstack application for a while now.你好 stackoverflow 社区,我创建自己的全栈应用程序已经有一段时间了。 on the NEXTjs framework, This is going pretty well., Unfortunately.在 NEXTjs 框架上,这很顺利。,不幸的是。 I got stuck on a JSON import object for a treeview component.我卡在了 JSON 导入 object 的 treeview 组件上。 The treeview component must be populated using a specific nested structure, along with which treeview item should be selected on an initial render.必须使用特定的嵌套结构填充 treeview 组件,并在初始渲染时选择 treeview 项目。

I managed to get the correct JSON object from the database, using a sql recursive tree function.我设法从数据库中得到正确的 JSON object,使用 sql 递归树 function。

const jsonObject = 

{
    "id": "bfa3fdf8-4672-404e-baf5-0f9098a5705b",
    "label": "main category 1",
    "children": [
        {
            "id": "12e544bc-91b1-4e5d-bdbc-2163a5618305",
            "label": "sub category 1.1",
            "children": []
        },
        {
            "id": "3f5e5cc7-f8b2-4d75-89e1-841c66d863e6",
            "label": "sub category 1.2",
            "children": [
                {
                    "id": "903a727f-d94d-44ff-b2f6-a985fd167343",
                    "label": "sub category 1.2.1",
                    "children": []
                },
                {
                    "id": "fb344480-8588-4ce3-9662-f8e89069e4b4",
                    "label": "sub category 1.2.2",
                    "children": []
                }
            ]
        }
    ]
}

The problem is that this object, with categories needs to be updated with a 'checked: "true"' or 'checked: "false"' key value pair based on the existence in the referenceSelectedCategories array.问题是这个 object,需要根据 referenceSelectedCategories 数组中的存在,使用“checked:“true””或“checked:“false””键值对更新类别。 And I don't know how to do this;我不知道该怎么做; maintaining the structure and object as needed.根据需要维护结构和 object。

const desiredOutputJsonObject = 

{
    "id": "bfa3fdf8-4672-404e-baf5-0f9098a5705b",
    "label": "main category 1",
**  "checked": "false",**
    "children": [
        {
            "id": "12e544bc-91b1-4e5d-bdbc-2163a5618305",
            "label": "sub category 1.1",
**          "checked": "true",**
            "children": []
        },
        {
            "id": "3f5e5cc7-f8b2-4d75-89e1-841c66d863e6",
            "label": "sub category 1.2",
**          "checked": "false",**
            "children": [
                {
                    "id": "903a727f-d94d-44ff-b2f6-a985fd167343",
                    "label": "sub category 1.2.1",
**                  "checked": "false",**                    
                    "children": []
                },
                {
                    "id": "fb344480-8588-4ce3-9662-f8e89069e4b4",
                    "label": "sub category 1.2.2",
**                  "checked": "true",**   
                    "children": []
                }
            ]
        }
    ]
}
const referenceSelectedCategories = 
[
    {
        "categoryId": "12e544bc-91b1-4e5d-bdbc-2163a5618305",
        "productId": "efed1c38-391b-4b5a-a9f1-91f3faec5f44",
        "Id": "f82b0f63-3f39-486c-9157-5c7683b8e3b2"
    },
    {
        "categoryId": "fb344480-8588-4ce3-9662-f8e89069e4b4",
        "productId": "efed1c38-391b-4b5a-a9f1-91f3faec5f44",
        "Id": "b2e8681b-eec4-404d-8f87-c6314db42e30"
    }
]

I've read several stackoverflow questions, also searched for examples, but can't get it to work.我已经阅读了几个 stackoverflow 问题,还搜索了示例,但无法正常工作。 Could someone help me out here?有人可以帮我吗?

Some extra information:一些额外的信息:

  • Code language I'm using is REACT on NEXTjs framework;我使用的代码语言是 NEXTjs 框架上的 REACT;
  • Treeview component could have a dept of max 5 levels; Treeview 组件可以有最多 5 个级别的部门;
  • The structure of the JSON object doesn't change, it's exactly as presented above. JSON object 的结构没有变化,完全如上所示。
  • The "id" in the JSON object corresponds to the "categoryId" in the array. JSON object中的“id”对应数组中的“categoryId”。
  • Do you need more information?你需要更多的信息? :) Just ask, I'll provide you with the extra details! :) 只要问,我会为您提供额外的细节!

Kind Regards,亲切的问候,

Chris克里斯

A straight forward solution with recursive method.使用递归方法的直接解决方案。 Done a quick test, working fine.做了一个快速测试,工作正常。 If any issues found, please point it out.如果发现任何问题,请指出。

    const parentObj = 
    [
        {
            "categoryId": "12e544bc-91b1-4e5d-bdbc-2163a5618305",
            "productId": "efed1c38-391b-4b5a-a9f1-91f3faec5f44",
            "Id": "f82b0f63-3f39-486c-9157-5c7683b8e3b2"
        },
        {
            "categoryId": "fb344480-8588-4ce3-9662-f8e89069e4b4",
            "productId": "efed1c38-391b-4b5a-a9f1-91f3faec5f44",
            "Id": "b2e8681b-eec4-404d-8f87-c6314db42e30"
        }
    ]
    
    const existingId = parentObj.map((item)=> (item.Id))
    
    const childobj = 
    
    {
        "id": "bfa3fdf8-4672-404e-baf5-0f9098a5705b",
        "label": "main category 1",
        "children": [
            {
                "id": "12e544bc-91b1-4e5d-bdbc-2163a5618305",
                "label": "sub category 1.1",
                "children": []
            },
            {
                "id": "3f5e5cc7-f8b2-4d75-89e1-841c66d863e6",
                "label": "sub category 1.2",
                "children": [
                    {
                        "id": "903a727f-d94d-44ff-b2f6-a985fd167343",
                        "label": "sub category 1.2.1",
                        "children": []
                    },
                    {
                        "id": "f82b0f63-3f39-486c-9157-5c7683b8e3b2",
                        "label": "sub category 1.2.2",
                        "children": []
                    }
                ]
            }
        ]
    }
const childObj = [childobj]
    const updateData=(obj)=> {
        if(existingId.includes(obj.id)) obj['checked'] = true; else obj['checked'] = false
    }
    
    const traverse=(childObj)=> {
        for(const obj of childObj) {
            updateData(obj);
            if(obj.children.length > 0) traverse(obj.children); 
        }
    }
    
    traverse(childObj); 

Here you can ty this logic:在这里你可以使用这个逻辑:

 let desiredOutputJsonObject = { id: "bfa3fdf8-4672-404e-baf5-0f9098a5705b", label: "main category 1", checked: false, children: [ { id: "12e544bc-91b1-4e5d-bdbc-2163a5618305", label: "sub category 1.1", checked: true, children: [], }, { id: "3f5e5cc7-f8b2-4d75-89e1-841c66d863e6", label: "sub category 1.2", checked: false, children: [ { id: "903a727f-d94d-44ff-b2f6-a985fd167343", label: "sub category 1.2.1", checked: false, children: [], }, { id: "fb344480-8588-4ce3-9662-f8e89069e4b4", label: "sub category 1.2.2", checked: true, children: [ { id: "fb344480-8588-4ce3-9662-f8e89069e4b9", label: "sub category 1.2.2", checked: false, children: [], }, ], }, ], }, ], }; let referenceSelectedCategories = [ { categoryId: "12e544bc-91b1-4e5d-bdbc-2163a5618305", productId: "efed1c38-391b-4b5a-a9f1-91f3faec5f44", Id: "f82b0f63-3f39-486c-9157-5c7683b8e3b2", }, { categoryId: "fb344480-8588-4ce3-9662-f8e89069e4b4", productId: "efed1c38-391b-4b5a-a9f1-91f3faec5f44", Id: "b2e8681b-eec4-404d-8f87-c6314db42e30", }, { categoryId:"fb344480-8588-4ce3-9662-f8e89069e4b9", productId: "efed1c38-391b-4b5a-a9f1-91f3faec5f44", Id: "b2e8681b-eec4-404d-8f87-c6314db42e30", }, ]; let stack = [desiredOutputJsonObject]; while (stack.length) { let desiredOutput = stack.pop(); if (desiredOutput.children) { desiredOutput.children.forEach((node) => { //get node whose id == category id let result = referenceSelectedCategories.find( (obj) => obj.categoryId === node.id ); // while traversing if we get referenceSelectedCategories.categoryId ==desiredOutputJsonObject.id if (result) { node.checked = true; } // for traverse purpose if (node.children.length) { stack.push(node); } }); } } console.log(desiredOutputJsonObject);

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

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