【问题标题】:How do I return specific field of nested object array in angular having fields value?如何以具有字段值的角度返回嵌套对象数组的特定字段?
【发布时间】:2019-07-07 06:32:09
【问题描述】:

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

这是我的数组:

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 字段值,但我发现我的过滤器应该包含多个字段。

【问题讨论】:

标签: arrays angular typescript object


【解决方案1】:

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

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

然后获取placeholder 字段:

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

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

【讨论】:

    【解决方案2】:

    这是我的解决方案:

      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;
      }
    

    【讨论】:

      猜你喜欢
      • 2021-08-10
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 2022-11-07
      • 1970-01-01
      • 2021-10-04
      • 1970-01-01
      相关资源
      最近更新 更多