簡體   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