weggi_swa weggi_swa - 3 months ago 30
TypeScript Question

Define arrays within array in Angular 2 and Typescript

In Angular 2 I was wondering if it was possible to define an array that has multiple other arrays in it. It is probably easier to just show what I mean:

I started out using this:

export class PaymentDetails {
account: any[];
bpNumber: number;
}


but this gave me the problem that when I populated it I couldn't really access the data in the account array, as I want more arrays inside of it.

So now i would like to define my class like this:

export class PaymentDetails {
account: [
debitAccount: [
{
debitAccountId: string;
debitBankCode: string;
debitBankName: string;
debitCountryCode: string;
debitAccountNumber: string;
debitAccountHolder: string;
debitContinous: string;
debitDueDate: string;
iban: string;
bic: string;
}
],
ccAccount: [
{
ccAccountId: string;
ccCompanyCode: string;
ccNumber: string;
ccStart: string;
ccExpiry: string;
ccDsbTransactionId: string;
ccCardholderName: string
}
]
];
bpNumber: number;

}


Is this at all possible?

The class is getting populated with this InMemoryDataService

export class InMemoryDataService {
createDb() {
let paymentDetailsDB = [
{
account: [
{
debitAccount: [
{
debitAccountId: '8736583',
debitBankCode: '45345',
debitBankName: 'KSK HGTT',
debitCountryCode: 'DE',
debitAccountNumber: '123453463',
debitAccountHolder: 'A Berg',
debitContinous: '',
debitDueDate: '',
iban: 'DE12344235',
bic: '324645',
},
{
debitAccountId: '6567456',
debitBankCode: '55463453',
debitBankName: 'GRDFE',
debitCountryCode: 'DE',
debitAccountNumber: '000123492',
debitAccountHolder: 'A Berg',
debitContinous: '',
debitDueDate: '',
iban: 'DE43523453',
bic: '123547665',
}
],

ccAccount: [
{
ccAccountId: '23413',
ccCompanyCode: '254345',
ccNumber: '238857827368837',
ccStart: '2010-10-05',
ccExpiry: '2018-10-05',
ccDsbTransactionId: '235231',
ccCardholderName: 'Anne Berg',
}
],
}
],
bpNumber: 4711,
}
];
return {paymentDetailsDB};
}
}

Answer

Your definition should look like this:

anonymous type variant:

export class PaymentDetails {
  accounts:
  {
    debitAccounts:
    {
      debitAccountId: string;
      debitBankCode: string;
      debitBankName: string;
      debitCountryCode: string;
      debitAccountNumber: string;
      debitAccountHolder: string;
      debitContinous: string;
      debitDueDate: string;
      iban: string;
      bic: string;
    }[],

    ccAccounts:
    {
      ccAccountId: string;
      ccCompanyCode: string;
      ccNumber: string;
      ccStart: string;
      ccExpiry: string;
      ccDsbTransactionId: string;
      ccCardholderName: string
    }[],

    bpNumber: number;
  };
}

named types (you should use this! :])

export class DebitAccount {
  Id: string;
  BankCode: string;
  BankName: string;
  CountryCode: string;
  Number: string;
  Holder: string;
  Continous: string;
  DueDate: string;
  iban: string;
  bic: string;
}

export class CcAccount {
  Id: string;
  CompanyCode: string;
  Number: string;
  Start: string;
  Expiry: string;
  DsbTransactionId: string;
  CardholderName: string
}

export class Account {
  debitAccounts: DebitAccount[];
  ccAccounts: CcAccount[];
  bpNumber: number;
}

export class PaymentDetails {
  account: Account[];
}
Comments