Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将一个JSON对象保存到多个JSON对象类型脚本_Json_Angular_Typescript - Fatal编程技术网

如何将一个JSON对象保存到多个JSON对象类型脚本

如何将一个JSON对象保存到多个JSON对象类型脚本,json,angular,typescript,Json,Angular,Typescript,现在,我从后端获取一个JSON对象formDocform数据 { "components": [ { "label": "Textfield1", "type": "textfield", "key": "textfield1", "input": true }, {

现在,我从后端获取一个JSON对象
formDoc
form数据

      {
        "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
                },


如何在Typescript中进行拆分,因为第一个将拆分为两个不同的对象,第二个将拆分为三个不同的对象?问题不在于拆分组件,而在于如何保存它,因为我不能像字符串一样创建“对象”列表。

你确定你不认为这比实际情况更复杂吗?您想要分开的每件事都很简单:

jsonObj.components[0]
jsonObj.components[1]
...
jsonObj.components[jsonObj.components.length - 1]

我不太明白你的问题,但我理解这两种选择:

//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
如果您有两个不同的对象(在不同的常量/变量中)


对不起,我没有解释清楚这个问题。我面临的问题是如何将拆分的组件保存为新对象。
//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