Craig Liston
Craig Liston

Reputation: 93

Angular interface with nested arrays

I'm new to angular and I'm coming from java, so I'm used to setting up class as data structures for my data. After some research I learn I should be using interfaces but I'm have a problem figuring out how I can set up an interface with nested arrays. Each of the arrays are just field / header pairs for creating a table. The object will be an array of them. But I have two tables that will have a two row header and I'm trying to figure out how to structure the interface.

interface TableHeaderDetails {
    field: string;
    header: string;
    date?: <what goes here>;
}

forecastCol: TableHeaderDetails[];

this.forecastCol = [
    { field: 'cumulativeExpected', header: 'Cumulative Expected ' },
    { field: 'cumulativeReceived', header: 'Cumulative Received' },
    { date : [
        { field: 'forecastYearMonth', header: 'Year - Month' },
            { details: [
                { field: 'expected', header: 'Expected' },
                { field: 'received', header: 'Received' }
            ]}
    ]}
];

Upvotes: 2

Views: 2425

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222720

You can use json2ts helper site to convert your JSON object to an interfaces which looks like as follows,

declare module namespace {

    export interface Detail {
        field: string;
        header: string;
    }

    export interface Date {
        field: string;
        header: string;
        details: Detail[];
    }

    export interface RootObject {
        field: string;
        header: string;
        date: Date[];
    }

}

Upvotes: 7

Related Questions