3

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};
  }
}
2
  • 2
    valid javascript code == valid typescript code. Check this out: stackoverflow.com/questions/966225/… Commented Aug 18, 2016 at 12:04
  • Looking at your code I see the following: (1) your class definition says "I want my array to have properties" but (2) your code that populates paymentDetailsDB declares that "each array item has child arrays". JS does not allow #1 but #2 is perfectly valid (mxii wrote an example of #2 implementation below) Commented Aug 18, 2016 at 13:09

2 Answers 2

4

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[];
}
Sign up to request clarification or add additional context in comments.

2 Comments

seems that the end result is the same (or at least the json scructure is the same when I display it in the console). However this is way more elegant and easier to understand. Turned out, my initial Problem was that i was accessing the arrays and objects inside wrongly...this post helped a lot with that stackoverflow.com/questions/11922383/…
@mxii sorry for asking a question in comment box, bcoz i am ban to ask question on this plateform. how can i access this array variables [Array(2)] 0: Array(2) 0: {id: 5, name: "Eternis", …} 1: {id: 4, name: "Sprenza", …} length: 2 __proto__: Array(0) length: 1 __proto__: Array(0). i am trying this way *ngFor="let item of bookedItems" . but i failed any suggestion?
1

Nested array could be defined as the following:

  items = [
     {name:'xyz', subMenuItem:[{name:'abc'}, {name:'cde'}], icon:'home'},
     {name:'pqr', subMenuItem:[{name:'abc'}, {name:'cde'}], icon:'home'},
    ];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.