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};
  }
}
weggi_swa
  • 330
  • 1
  • 5
  • 12
  • 2
    valid javascript code == valid typescript code. Check this out: http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – Dylan Meeus Aug 18 '16 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) – Volodymyr Usarskyy Aug 18 '16 at 13:09

2 Answers2

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[];
}
slaesh
  • 16,659
  • 6
  • 50
  • 52
  • 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 http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – weggi_swa Aug 19 '16 at 06:33
  • @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? – Rahul Verma Nov 22 '19 at 02:21
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'},
    ];
Fabien
  • 4,862
  • 2
  • 19
  • 33
mohit uprim
  • 5,226
  • 2
  • 24
  • 28