json - 如何将 JSON 对象保存到多个 JSON 对象 - TypeScript
问题描述
现在我正在formDoc
从后端获取 JSON 对象表单数据。
{
"components": [
{
"label": "Textfield1",
"type": "textfield",
"key": "textfield1",
"input": true
},
{ "label": "Radio",
"type": "radiobutton",
"key": "radiobutton1",
"input": true
},]}
我得到的另一种形式是
{
"components": [
{
"label": "Text2",
"type": "textfield",
"key": "textfield2",
"input": true
},
{ "label": "Checkbox",
"type": "checkbox",
"key": "checkbox1",
"input": true
},
{ "label": "Checkbox2",
"type": "checkbox",
"key": "checkbox2",
"input": true
},]}
由于表单是由用户自定义的,因此不同的表单具有不同的组件。我正在尝试按键拆分 JSON。例如,将第一个拆分为
{
"label": "Textfield1",
"type": "textfield",
"key": "textfield1",
"input": true
},
和
{ "label": "Radio",
"type": "radiobutton",
"key": "radiobutton1",
"input": true
},
如何在 Typescript 中进行拆分,因为第一个将拆分为两个不同的对象,第二个拆分为三个不同的对象?问题不在于拆分组件,而是如何保存它的任何想法,因为我无法像字符串一样制作“对象”列表。
解决方案
我不完全明白你的问题,但我理解这两个选项:
//Lets call theObject to your object
const newArray = [];
theObject.forEach(item => item.components.forEach(i => newArray.push(i)));
// now you can do whatever you want with your flat array
如果您有两个不同的对象(在不同的常量/变量中)
const newArray = [...object1.components, ...object2.components];
// now you can do whatever you want with your flat array
推荐阅读
- javascript - Javascript字符串替换问题
- python-3.x - 由于“禁止的内部错误代码”,构建管道失败
- azure-active-directory - 可以绕过单个功能的 Azure Function App Azure AD 身份验证并改为通过功能键公开它吗?
- elixir - 在应用程序中获取 elixir mix 发布版本
- ruby-on-rails - React:在组件中使用 Get Request 的结果(Ruby on Rails + CoffeeScript)
- bash - /bin/sh 不识别十六进制转义序列
- android - 将自定义列表视图从 MainActivity 更改为 ExampleFragment
- python - 将数据附加到 DataFrame
- php - 有没有一种简单的方法来创建一个在 session_destroy() 之后存在的变量?
- javascript - React Swiper JS 控制器