2

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' }
            ]}
    ]}
];
2
  • What is details in your this.forecastCol Commented Mar 3, 2019 at 10:30
  • Please add how you would have typed this in Java. The data type structure is unclear. Commented Mar 3, 2019 at 14:23

1 Answer 1

7

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[];
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the link that will help immensely. This was a late night coding exercise and I realized I needed to make a more layered object instead of trying to use the base object for all the pieces.

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.