Javascript 从关键点创建嵌套对象

Javascript 从关键点创建嵌套对象,javascript,Javascript,我需要从json结构创建一个对象。我对如何进行结构的递归感到困惑 这是我的json结构示例 { "dynamic.title": "Lorem Ipsum title", "dynamic.main_section.header.title": "Lorem Ipsum main section header title ", "dynamic.main_se

我需要从json结构创建一个对象。我对如何进行结构的递归感到困惑

这是我的json结构示例

{
        "dynamic.title": "Lorem Ipsum title",
        "dynamic.main_section.header.title": "Lorem Ipsum main section header title ",
        "dynamic.main_section.body.collection.first.title": "Lorem Ipsum main section  body collection first title",
        "dynamic.main_section.body.collection.first.description": "Lorem Ipsum main section  body collection first description",
        "dynamic.main_section.body.collection.first.id": "Lorem Ipsum main section  body collection first id",
        "dynamic.main_section.body.collection.first.child.first.title": "Lorem Ipsum main section  body collection first of first child title ",
        "dynamic.main_section.body.collection.first.child.first.id": "Lorem Ipsum main section  body collection first of first child id ",
        "dynamic.main_section.body.collection.first.child.first.url": "https://loremipsum/1",
        "dynamic.main_section.body.collection.first.child.first.names.first": "John Doe",
        "dynamic.main_section.body.collection.first.child.first.names.second": "Jane Doe",
        "dynamic.main_section.body.collection.second.title": "Lorem Ipsum main section  body collection second title",
        "dynamic.main_section.body.collection.second.description": "Lorem Ipsum main section  body collection second description",
        "dynamic.main_section.body.collection.second.id": "Lorem Ipsum main section  body collection second id",
        "dynamic.main_section.body.collection.second.child.first.title": "Lorem Ipsum main section  body collection second of first child title ",
        "dynamic.main_section.body.collection.second.child.first.id": "Lorem Ipsum main section  body collection second of first child id ",
        "dynamic.main_section.body.collection.second.child.first.url": "https://loremipsum/2",
        "dynamic.main_section.body.collection.second.child.second.title": "Lorem Ipsum main section  body collection second of second child title ",
        "dynamic.main_section.body.collection.second.child.second.id": "Lorem Ipsum main section  body collection second of second child id ",
        "dynamic.main_section.body.collection.second.child.second.url": "https://loremipsum/3"
    }
我需要的格式是这样的

{
        "dynamic": {
            "title": "Lorem Ipsum title",
            "main_section": {
                "header": {
                    "title": "Lorem Ipsum main section header title "
                },
                "body": {
                    "collection": [
                        {
                            "title": "Lorem Ipsum main section  body collection first title",
                            "description": "Lorem Ipsum main section  body collection first description",
                            "id": "Lorem Ipsum main section  body collection first id",
                            "child": [
                                {
                                    "title": "Lorem Ipsum main section  body collection first of first child title ",
                                    "id": "Lorem Ipsum main section  body collection first of first child id ",
                                    "url": "https://loremipsum/1",
                                    "names": ["John Doe", "Jane Doe"]
                                }
                            ]
                        },
                        {
                            "title": "Lorem Ipsum main section  body collection second title",
                            "description": "Lorem Ipsum main section  body collection second description",
                            "id": "Lorem Ipsum main section  body collection second id",
                            "child": [
                                {
                                    "title": "Lorem Ipsum main section  body collection second of first child title ",
                                    "id": "Lorem Ipsum main section  body collection second of first child id ",
                                    "url": "https://loremipsum/2"
                                },
                                {
                                    "title": "Lorem Ipsum main section  body collection second of second child title ",
                                    "id": "Lorem Ipsum main section  body collection second of second child id ",
                                    "url": "https://loremipsum/3"
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
如果左侧键由第一个和第二个键组成,则它将是一个数组,但如果存在多个属性,则它将是一个对象,否则它可能是一个数组


如何在js中实现这一点?

您需要将数字替换为索引,并检查键是否包含对象或数组

const
setValue=(对象、路径、值)=>{
常数
索引={first:0,second:1},
keys=path.replace(新的RegExp(Object.keys(index).join('|'),'g'),k=>index[k])。split('.'),
last=keys.pop();
钥匙
.reduce((o,k,i,kk)=>o[k]??=isFinite(k中的i+1?kk[i+1]:last)?[]:{},对象)
[最后]=价值;
返回对象;
},
数据={“dynamic.title”:“Lorem Ipsum title”,“dynamic.main_section.header.title”:“Lorem Ipsum main section header title”,“dynamic.main_section.body.collection.first.title”:“Lorem Ipsum main section body collection第一个标题”,“dynamic.main_section.body.collection.first.description”:“Lorem Ipsum主体部分主体集合第一个描述”,“dynamic.main_section.body.collection.first.id:“Lorem Ipsum主体部分主体集合第一个id”,“dynamic.main_section.body.collection.first.child.first.title:“Lorem Ipsum主体部分主体集合第一个子标题的第一个”dynamic.main\u section.body.collection.first.child.first.id:“Lorem Ipsum main section body collection-first-of-first-child-id”,“dynamic.main\u section.body.collection.first.child.first.url:”https://loremipsum/1,“dynamic.main_section.body.collection.first.child.first.names.first”:“John Doe”dynamic.main\u section.body.collection.first.child.first.names.second:“Jane Doe”,“dynamic.main\u section.body.collection.second.title:“Lorem Ipsum main section body collection第二标题”,“dynamic.main\u section.body.collection.second.description:“Lorem Ipsum main section body collection第二描述”dynamic.main\u section.body.collection.second.id:“Lorem Ipsum main section body collection second id”,“dynamic.main\u section.body.collection.second.child.first.title:“Lorem Ipsum main section body collection second子标题的第二个”dynamic.main\u section.body.collection.second.child.first.id:“Lorem Ipsum主体部分主体集合第一个子id的第二个“,”dynamic.main_section.body.collection.second.child.first.url”:”https://loremipsum/2,“dynamic.main_section.body.collection.second.child.second.title”:“Lorem Ipsum main section body collection second of second child title”dynamic.main_section.body.collection.second.child.second.id:“Lorem Ipsum main section body collection second of second child id”,“dynamic.main_section.body.collection.second.child.second.url:”https://loremipsum/3" },
结果=对象
.条目(数据)
.reduce((r[k,v])=>setValue(r,k,v),{});
console.log(结果);

.as console wrapper{max height:100%!important;top:0;}
我向您展示了转换数据的方法,以便您继续使用它,甚至在必要时实现自动化

let数据={
“dynamic.title”:“Lorem Ipsum title”,
“dynamic.main_section.header.title”:“Lorem Ipsum main section header title”,
“dynamic.main_section.body.collection.first.title”:“Lorem Ipsum main section body collection first title”
}
dataTrans={}
//数据[“dynamic.title”]
dataTrans.dynamic={}
dataTrans.dynamic.title=数据[“dynamic.title”]
//数据[“dynamic.main_section.header.title”]
dataTrans.dynamic.main_节={}
dataTrans.dynamic.main_section.header={}
dataTrans.dynamic.main_section.header.title=数据[“dynamic.main_section.header.title”]
dataTrans.dynamic.main_section.body={}
dataTrans.dynamic.main_section.body.collection=[]
dataTrans.dynamic.main_section.body.collection[0]={}
dataTrans.dynamic.main\u section.body.collection[0]。title=data[“dynamic.main\u section.body.collection.first.title”]

console.log(dataTrans)
为什么不使用索引而不是数字?您尝试过什么?这就是JSON结构不使用数字而不是第一秒的方式看起来不错,谢谢!谢谢,我会检查这个。需要了解会问如果对这个@Nina有任何疑问,我如何将object转换为JSONY的相反方式您可以自己尝试并询问如果您没有成功,请回答一个新问题。@dev,也许最后一条评论有点苛刻,但在评论中回答一个新问题并不合适。无论如何,如果您展示了一些方法,以从嵌套结构器获得一个简单结果,您可以就一个新问题寻求帮助。