【发布时间】:2018-01-08 09:37:48
【问题描述】:
我正在使用 JSON 模式中的 JavaScript 动态构建的表单,如下所示:
{
"questionSets": [
{
"questionSetId": "example-fields",
"questions": [
{
"questionId": "text",
"question": "Text Field",
"input": {
"type": "textInput",
"default": ""
},
},
{
"questionId": "textarea",
"question": "Text Area",
"input": {
"type": "textareaInput",
"default": ""
}
}
]
}
]
}
提交表单时,它只返回如下所示的更新值:
{
text: "some entered text",
textarea: "some more entered text"
}
生成的 JSON 数组的键对应于第一个数组中的 questionId 和 default 键的值。
合并这两个数组的最佳方法是什么,结果是:
{
"questionSets": [
{
"questionSetId": "example-fields",
"questions": [
{
"questionId": "text",
"question": "Text Field",
"input": {
"type": "textInput",
"default": "some entered text"
},
},
{
"questionId": "textarea",
"question": "Text Area",
"input": {
"type": "textareaInput",
"default": "some more entered text"
}
}
]
}
]
}
【问题讨论】:
-
也许可以创建一个创建空数组的函数,并遍历结果并相应地附加到空数组?
标签: javascript json node.js