【发布时间】:2020-08-04 06:38:47
【问题描述】:
我有问题,我想将多个 parentId JSON 转换为单个 parentId JSON
这是我将提供给我的 js 函数的多个父 json 示例。
[{
"id": "1",
"name": "first_name",
"label": "First Name",
"placeholder": "Enter Your First Name",
"required": "yes",
"inputType": "text",
"options": [],
"parentId": "",
"isPost": true
},
{
"id": "4",
"name": "ready_to_provide_email_address",
"label": "Ready to Provide Email Address",
"placeholder": "Enter Your Phone Number",
"required": "yes",
"inputType": "radio",
"options": [{
"label": "Yes",
"name": "ready_to_provide_email",
"value": "Yes",
"id": "5"
},
{
"label": "No",
"name": "ready_to_provide_email",
"value": "No",
"id": "6"
}
],
"parentId": "",
"isPost": false
},
{
"id": "7",
"name": "email",
"label": "Email",
"placeholder": "Enter Your Email",
"required": "yes",
"inputType": "text",
"options": [],
"parentId": ["5", "6"],
"isPost": true
}
]
这是我想要输出的单父 json。
[{
"id": "1",
"name": "first_name",
"label": "First Name",
"placeholder": "Enter Your First Name",
"required": "yes",
"inputType": "text",
"options": [],
"parentId": "",
"isPost": true
},
{
"id": "4",
"name": "ready_to_provide_email_address",
"label": "Ready to Provide Email Address",
"placeholder": "Enter Your Phone Number",
"required": "yes",
"inputType": "radio",
"options": [{
"label": "Yes",
"name": "ready_to_provide_email",
"value": "Yes",
"id": "5"
},
{
"label": "No",
"name": "ready_to_provide_email",
"value": "No",
"id": "6"
}
],
"parentId": "",
"isPost": false
}, {
"id": "7",
"name": "email",
"label": "Email",
"placeholder": "Enter Your Email",
"required": "yes",
"inputType": "text",
"options": [],
"parentId": "5",
"isPost": true
},
{
"id": "8",
"name": "email",
"label": "Email",
"placeholder": "Enter Your Email",
"required": "yes",
"inputType": "text",
"options": [],
"parentId": "6",
"isPost": true
}
]
注意:在我的例子中,object.option.id 是 element 的父 id。请帮我。非常感谢您。
【问题讨论】:
-
你想如何从
parentId生成一个新的id? -
新 id 可以是任何唯一的数字,如主键
-
尝试使用
Array.flatMap()developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… -
当我做错数组时,请你帮忙,我会非常感激的
标签: javascript arrays json reactjs