Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to understand complicated code

I always get stuck with code of other people who use DRY method, which sounds too ood but hard to understand, take a example of the following

    mapSampleObjectToArray(SummarisedView) {
        const convertArray = [];
        for (const propertyIndex in SummarisedView) {
            convertArray.push({
                samplePart: SummarisedView[propertyIndex].samplePartNumber,
                sampleDescription: SummarisedView[propertyIndex].sampleDescription,
                individualQ: SummarisedView[propertyIndex].individualQ
                individualQu: SummarisedView[propertyIndex].individualQu
                individualAQ22Data: ReportSummarisedView[propertyIndex].individual
            });
        }
        return convertArray;
    }

I get lost of what is code doing especially

SummarisedView[propertyIndex].samplePartNumber,

like what is [propertyIndex] and why we use .SamplePartNumber

1 Answer

Brendon Butler
Brendon Butler
4,254 Points

I'm not that great in Javascript, however it looks as if there is an array of SummarisedViews. While propertyIndex is probably an integer of some sort marking the index. .samplePartNumber I assume would return a string containing a part number.

For example, lets say you have an array of 3 SummarisedViews, you would be able to select between them by using SummarisedView[0-2] (chose a number within these values). Once you have selected a SummarisedView, you call functions on that object to get data from it.

SummarisedView[0].sampleDescription might equal "Dog Toy."
SummarisedView[1].sampleDescription might equal "Cat Treats."

Basically what I did to understand it was to break it down into parts. I feel you may be able to get a better understanding of this if you were to take the Java Objects track. Hope this answer helps!