gpt4 book ai didi

javascript - 将新对象推送到嵌套对象

转载 作者:行者123 更新时间:2023-11-29 16:30:27 25 4
gpt4 key购买 nike

我正在尝试用 angular8 制作问卷。
有一个带有加号按钮的动态表单。您可以通过单击加号按钮添加新字段,该字段应添加到所选字段的下方。为此,我正在尝试制作嵌套 json 对象我想在特定对象之后再添加一个对象。
例如 - 我需要在 Id 为 3 的对象之后添加新对象

Here is what I have done so far

mainConatiners = [
{
page_title : "This is first page.",
"data":[
{
id:1,
title:"This is first question",
type:1,
page:0,
position:'0',
parent:'0'
},
{
id:2,
title:"This is second question",
type:2,
page:0,
position:'1',
parent:0,
"data":[
{
key:"[data][1][data][0]",
id:3,
title:"This is third question",
type:1,
page:0,
position:'1-0',
parent:2
},
{
id:4,
title:"This is fourth question",
type:2,
page:0,
position:'1-1',
parent:2,
"data":[
{
id:5,
title:"This is tenth question",
type:2,
page:0,
position:'1-1-0',
parent:'2-4'
}
]
}
]
}
]
}]

Expected output -

mainConatiners = [
{
page_title : "This is first page.",
"data":[
{
id:1,
title:"This is first question",
type:1,
page:0,
position:'0',
parent:'0'
},
{
id:2,
title:"This is second question",
type:2,
page:0,
position:'1',
parent:0,
"data":[
{
key:"[data][1][data][0]",
id:3,
title:"This is third question",
type:1,
page:0,
position:'1-0',
parent:2
},
{
key:"[data][1][data][0]",
id:12,
title:"This is third question",
type:1,
page:0,
position:'1-0',
parent:2
},
{
id:4,
title:"This is fourth question",
type:2,
page:0,
position:'1-1',
parent:2,
"data":[
{
id:5,
title:"This is tenth question",
type:2,
page:0,
position:'1-1-0',
parent:'2-4'
}
]
}
]
}
]
}]

最佳答案

这是可变的解决方案 - 我们首先将所有对象索引到 idHash 映射,其中包含有关对象 id、数据数组中的对象索引和 dara 数组本身(parentArr)的信息 - 我们找到对象 id=3 并在其后插入新的 10 个对象.

let idHash = {} // id=> hash object (hash obiect= {obj,index,parentArr)}
let makeHash = data => (data||[]).forEach((d,i)=>
(idHash[d.id]={ obj:d, index:i, parentArr: data}, makeHash(d.data)));

makeHash(mainConatiners);

let hobj = idHash[3] // hash object with object id=3 after which we want to insert
hobj.parentArr.splice(hobj.index+1,0,newObj);

mainConatiners = [{
page_title: "This is first page.",
"data": [{
id: 1,
title: "This is first question",
type: 1,
page: 0,
position: '0',
parent: '0'
},
{
id: 2,
title: "This is second question",
type: 2,
page: 0,
position: '1',
parent: 0,
"data": [{
key: "[data][1][data][0]",
id: 3,
title: "This is third question",
type: 1,
page: 0,
position: '1-0',
parent: 2
},
{
id: 4,
title: "This is fourth question",
type: 2,
page: 0,
position: '1-1',
parent: 2,
"data": [{
id: 5,
title: "This is tenth question",
type: 2,
page: 0,
position: '1-1-0',
parent: '2-4'
}]
}
]
}
]
}]

let newObj = {
key: "[data][1][data][0]",
id: 12,
title: "This is third question",
type: 1,
page: 0,
position: '1-0',
parent: 2
}

let idHash = {} // id=> hash object (hash obiect= {obj,index,parentArr)}
let makeHash = data => (data||[]).forEach((d,i)=>
(idHash[d.id]={ obj:d, index:i, parentArr: data}, makeHash(d.data)));

makeHash(mainConatiners);

let hobj = idHash[3] // hash object with object id=3 after which we want to insert
hobj.parentArr.splice(hobj.index+1,0,newObj);

console.log(mainConatiners)

关于javascript - 将新对象推送到嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58953923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com