1

I have a curriculum that doesn’t need to be changed dynamically so I want to hard code it in service in my angular project.

The data is as follows:

Term 1 Term 2 Term 3

Each term has 5 grades Each grade has 10 subjects Each subject has 12-16 weeks depending on the term.

I want to access the data as follow

currentLesson = term.grade.subject.week.data;

To do this, I believe I need to make a Term Array which each term consists of a grade array which then each grade consists of a subject Array which then each subject Array consists of An object of week / curriculumData.

I can make the last simple Array of objects but my mind shuts down trying to do the array of Array of Array of objects.

Or is there a better way to achieve this?

3
  • I'm assuming you really mean something like const currentLesson = curriculum.terms[t].grades[g].subjects[s].weeks[w].data where t,g,s, and w are numbers. But what are you asking here... how to define interfaces for these types? Or how to convert your curriculum data into this format? Or something else? Commented Aug 4, 2019 at 3:09
  • Thanks for the reply! In short, I don’t know how to declare an array of arrays of arrays of objects. Commented Aug 4, 2019 at 3:58
  • Right now my declaration looks like this but I’m not confident that it’s the best approach. interface curriculum { term: number; grade: Array<{ id: number; subject: Array<{ id: number; week: Array<{ id: number; data: string; }> }> }> } Commented Aug 4, 2019 at 4:18

1 Answer 1

2

If all of your 'data' is a simple string, you could simply create a multidimensional array like this:

const lessons: string[][][][] = buildLessons();
const currentLesson = lessons[t][g][s][w];

Of course, you could replace string with a more complicated datatype if you wanted to, e.g.:

interface Lesson {
  data: string;
  otherProperty: number;
}
const lessons: Lesson[][][][] = buildLessons();
const currentLesson = lessons[t][g][s][w];
// do something with currentLesson.data, etc

Alternatively, if you want to use an object structure, you could also write that out in a more readable way by splitting into separate interfaces, like so:

interface Curriculum {
  term: number;
  grades: Grade[];
} 

interface Grade {
  id: number;
  subjects: Subject[];
}

interface Subject {
  id: number;
  weeks: Week[];
}

interface Week {
  id: number;
  data: string;
}

let example: Curriculum;
const currentLesson = example.grades[g].subjects[s].weeks[w].data;
Sign up to request clarification or add additional context in comments.

2 Comments

The splitting interfaces is what I was after. Thanks so much!
You're welcome :) In that case can you please accept the answer?

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.