如何将所选对象从一组对象推入一组对象

问题描述

我有这个对象,其中包含许多元素,例如数组中的几个其他对象。

有两个数组:

objects1的数组:

enter image description here

里面就是这个

子窗体和部分

层次结构是:

[0]--
title
     |
     section [
             properties|
                       [[***target***]]
                                       |
                                       title
     ]
     subform [
             properties|
                       [[***target***]]
                                       |
                                       title
     ]
[1]--
     |
     section [
             properties|
                       [[***target***]]
                                       |
                                       title
     ]
     subform [
             properties|
                       [[***target***]]
                                       |
                                       title
     ]

我想做的是有选择地挑选标题,如下所示:

我像这样实例化私有obj:

private obj = {
  data: [{
    subform: '',section: '',field: ''
  }]
};

并运行for / next循环以填充obj OBJECT

    for (let i = 0; i < this.dataTableJSON.length; i++) {
      this.obj.data[i].subform = this.dataTableJSON[i].subform.properties.title;
      this.obj.data[i].section = this.dataTableJSON[i].section.properties.title;
      this.obj.data[i].field = this.dataTableJSON[i].title;
    }

发生的事情是,它大约运行1次,然后返回“ 1”并在错误控制台中显示

无法设置未定义的属性“子窗体”。

我寻求的结果就是这样:

private obj = {
  data: [{
    subform: 'Title 1',section: 'Section Title 1',field: 'Field Title 1'
  },{
    subform: 'Title 2',section: 'Section Title 2',field: 'Field Title 2'
  }]
};

为什么第二次死去?这很简单...我正在使用TYPESCRIPT

DAI更新:

戴,我在那里99%...

实现代码时,我编写了以下枚举。

export interface DataItem {
   subform: string;
   section: string;
   field: string;
};

export type ObjType = {
  data: DataItem[]
};

这是上面的导入

import {DataItem,ObjType} from '../../../services/datatables-integration-services/datatables-datatypes-enum';

在实际的component.ts文件中,我已经输入了:

 private readonly obj: ObjType = {
    data: []
 };

然后我按照您所说的实现了for / next循环。

    for (let i = 0; i < this.dataTableJSON.length; i++) {

      const datacopy: DataItem = {
        subform = this.dataTableJSON[i].subform.properties.title,section = this.dataTableJSON[i].section.properties.title,field   = this.dataTableJSON[i].title
      };

      this.obj.data[i] = datacopy;
    }

以下是我遇到的错误,并克服了我需要更改的错误

  subform = this.dataTableJSON[i].subform.properties.title,field   = this.dataTableJSON[i].title

对此

  subform: this.dataTableJSON[i].subform.properties.title,section: this.dataTableJSON[i].section.properties.title,field: this.dataTableJSON[i].title

正在测试

而且有效!嘘!

谢谢戴!

DAI更新:

我有一个错误

FINAL JSON必须看起来像这样

  {
    "data": [
      {
        field: "Social Security number"
        required: true
        section: "Employee information"
        subform: "Personal information"
      },{
        field: "Country of issuance"
        required: true
        section: "Eligibility information"
        subform: "Employment Eligibility"
      }
    ]
  }

对不起,戴...再次感谢

enter image description here

enter image description here

解决方法

更改此:

    for (let i = 0; i < this.dataTableJSON.length; i++) {
      this.obj.data[i].subform = this.dataTableJSON[i].subform.properties.title;
      this.obj.data[i].section = this.dataTableJSON[i].section.properties.title;
      this.obj.data[i].field = this.dataTableJSON[i].title;
    }

对此:

    // Put these `type` declarations somewhere appropriate in your codebase:

    interface DataItem = {
        subform: string;
        section: string;
        field  : string;  
    };

    type ObjType = {
        data: DataItem[]
    };

    // Change your `obj` member to this:
    private readonly obj: ObjType = {
        data: []
    };

    // And change your for loop to this:
    for (let i = 0; i < this.dataTableJSON.length; i++) {
        
        const dataCopy : DataItem = {
            subform: this.dataTableJSON[i].subform.properties.title,section: this.dataTableJSON[i].section.properties.title,field  : this.dataTableJSON[i].title;
        };

        this.obj.data[i] = dataCopy;
    }

您也可以执行此操作(语法上较短,但是隐藏了类型信息,这可能会使TypeScript和JavaScript的新手感到困惑,这些人可能不了解正在发生的事情)

    for (let i = 0; i < this.dataTableJSON.length; i++) {
        
        this.obj.data[i] = {
            subform: this.dataTableJSON[i].subform.properties.title,field  : this.dataTableJSON[i].title;
        };
    }

这可以在语法上简化为:

    for (let i = 0; i < this.dataTableJSON.length; i++) {
        const d = this.dataTableJSON[i];
        this.obj.data[i] = {
            subform: d.subform.properties.title,section: d.section.properties.title,field  : d.title;
        };
    }