1

I have code like below:

        ngOnInit() {
    this.tenantPackagesService.getTenantPackages().takeUntil(this.destroyed).subscribe();
    this.tenantPackagesService.tenantPackagesObs.filter(packages => !!packages).takeUntil(this.destroyed).subscribe(packages => {

        this.selectedPackage;

        let flags = {};
        this.packages = packages
            .filter(function (entry) {
                if (flags[entry.id]) {
                    return false;
                }
                flags[entry.id] = true;
                return true;
            })
            .map(o => { return { label: `${o.id}`, value: o } });

        // Versions of package
        this.versions = packages
            // Here I need filter 
            .map(o => { return { label: `${o.version}`, value: o } });
    })
}

There are an array of objects with packages. Each of package has version. There are a lot of packages with the same name but different version. I filtered packages by unique name and now I need to map different of version selected package (this.selectedPackage). So, if I choose package1 I need to get only versions associated to that package. How to do this with .filter method? Is it possible?

Italik
  • 696
  • 2
  • 18
  • 35

1 Answers1

1

Try this:

const filteredPackage = this.versions.filter(function (el) {
  return el.version === this.selectedPackage;
});

More details on how to use .filter:

https://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes