首页 > 解决方案 > 如何以具有字段值的角度返回嵌套对象数组的特定字段?

问题描述

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

这是我的数组:

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带有Brandsvalue的columnTitle字段和带有brandURLvalue 的字段来找到它?

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

标签: arraysangulartypescriptobject

解决方案


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

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

然后获取placeholder字段:

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

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

推荐阅读