繁体   English   中英

如何以角度具有字段值的形式返回嵌套对象数组的特定字段?

[英]How do I return specific field of nested object array in angular having fields value?

我有这样的嵌套对象数组。

这是我的数组:

public collections: ICollections[] = [
  {
    collectionName: 'Brands',
    collectionFields: [
      {
        columnTitle : 'brandTitle',
        Type : dtEnum.string,
        control: {
          controlTitle: controlsEnum.input,
          controlType: controlsEnum.input,
          controlProperties: 
            {
              placeholder: 'Enter brand title here ...',
              type: 'text',
              autocomplete: false,
            }
        },
        columnWidth: 200
      }
    ],
    collectionFieldValidation: [{name: 'test'}],
    hasPaginator: true,
    stickyColumn: 0,
    stickyHeader: true
  },
    {
      columnTitle : 'brandURL',
      Type : dtEnum.string,
      control: {
        controlTitle: controlsEnum.input,
        controlType: controlsEnum.input,
        controlProperties: {
          placeHolder: 'Enter Brand URL',
          type: 'text',
          autocomplete: false,
        }
      },
      columnWidth: 300
    },
    {
      columnTitle : 'brandDescription',
      Type : dtEnum.string,
      control: {
        controlTitle: controlsEnum.textarea,
        controlType: controlsEnum.textarea,
        controlProperties: {
          placeHolder: 'Enter Brand Description',
          type: 'text',
          autocomplete: false,
        }
      },
      columnWidth: 300
    }
];

我想到达placeholder字段。 如何通过仅具有Brands值的collectionName字段和具有brandURL值的columnTitle字段来找到它?

这个问题以前只是用collectionName字段值问的,但我发现我的过滤器应包含多个字段。

首先,找到与“品牌”或其他任何东西相对应的集合:

let result = collections.find(p => p.collectionName === "Brands");

然后获取placeholder字段:

your_index更改为0或您的特定索引

if (result) {
    let placeholder = result.collectionFields[your_index].control.controlProperties.placeholder;
}

这是我的解决方案:

  placeholder_finder(collectionSearchKey: string, fieldSearchKey: string): string {
    let field: any;
    let placeholder: string;
    const obj = this.genInfo.collections.filter(
      x => x.collectionName === collectionSearchKey
    );
    obj.forEach(data => {
      field = data.collectionFields.filter(
        x => x.columnTitle === fieldSearchKey
      );
    });
    field.forEach(element => {
      placeholder = element.control.controlProperties.placeHolder;
    });
    return placeholder;
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM