K
K
Konstantin2020-07-06 15:55:35
JavaScript
Konstantin, 2020-07-06 15:55:35

How can the code section be improved?

ngAfterViewInit() {
        combineLatest(...this.filtersList.map((f) => f.filtersChanges)).subscribe(
            (selectedFilters) => {
                const filterUrlCombiner = new FilterUrlCombiner();
                const filterUrlBuilders = selectedFilters.map((filter) => new FilterUrlBuilder(filter, ComparingOperation.eq));
                filterUrlBuilders.forEach((filter) => {
                    filter.buildStringUrl();
                });

                const filterUrls = filterUrlBuilders.filter((filter) => filter.url).map((filter) => filter.url);
                filterUrlCombiner.combineUrl(filterUrls);

                this.filterBy = filterUrlCombiner.getUrl();
                this.settingsService.setDistributionFiltersSettings(selectedFilters);
                setTimeout(
                    () => (this.buttonDisabled = !selectedFilters.some((item) => item && item.selected && item.selected.length > 0)),
                );
            },
            (error) => {
                console.log('ERROR: ' + error);
            },
        );
    }


The input code has an array of selectedFilters values , then it maps each FilterUrlBuilder array , and then generates a buildStringUrl value for each FilterUrlBuilder element .

At the end, it gives an array of generated urls to the class: And
filterUrlCombiner.combineUrl(filterUrls);

after that, assigns the result to a variable:

this.filterBy = filterUrlCombiner.getUrl();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2020-07-06
@Junart1

filterUrlBuilders.filter((filter) => filter.url).map((filter) => filter.url);

This is usually done like this:
filterUrlBuilders.map(filter => filter.url).filter(Boolean);

In general, the whole question is in the classes FilterUrlCombinerand FilterUrlBuilder: are they really needed and do some useful work or does someone have an enterprise brain?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question