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

时间:2019-07-07 06:32:09

标签: arrays angular typescript object

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

这是我的数组:

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字段。如何通过仅具有collectionName值的Brands字段和具有columnTitle值的brandURL字段来找到它?

此问题仅用collectionName字段值问过,但我发现我的过滤器应包含多个字段。

2 个答案:

答案 0 :(得分:1)

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

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

然后获得placeholder字段:

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

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

答案 1 :(得分:0)

这是我的解决方案:

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