Angular: Optimal Object Structure Structures

I am working in Angular, currently focusing on filtering a nested object. Here is the structure of my object:

$scope.subjectBin = {
     "Faculty of Engineering": {
           "ECE": [{<course-object>},{<course-object>}],
           "CHEM: [{<course-object>}]
      },
     "Faculty of Science": {
           "BIOL: [{<course-object>},...],
           ...
      },
      ...
}

      

When I work with filtering this object, create new functions and render this in a layered accordion, I think I need to redesign this object.

Someone suggested changing the object to the following:

$scope.subjectBin = [{
    faculty: "Faculty of Engineering",
    subjects: [{
        subjectName: "ECE",
        courses: [{<course-object>},{<course-object>}]
        },
        ...
    ]
}, {
    faculty: "Faculty of Science",
    subjects: [{
        subjectBin: "CMPUT",
        courses: [{<course-object>},...]
        },
        ...
    ]
}]

      

I was told this layout is better because: "it doesn't use objects as layouts and doesn't match the pattern."

I agree that this is better, but would like some clarification regarding the layout of the object in general.

What are the best practices for nesting nested objects ? Are massive objects with fixed key names preferred?

+3


source to share


1 answer


The second example is much better.

In the first example, you have a list of objects with different string keys (no hardcoding). The problem is that these keys which are strings must be escaped to remove tags, commas, or other problematic invalid keystrings.

The second example is much better since you have an array of fixed string objects with hard coded strings. When you define your keys, you will always know what properties this object has and what content each property has.



You should always strive to write objects like this:

test = {
    property1 : {},
    property2 : [],
    property3 : value
}

      

where property1, property2 and property3 are hardcoded by you to represent their specific content.

+1


source







All Articles