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?

1
  • 2
    If it's a kind of groupBy you need, i suggest you to take a look to lodash and the groupBy function. Commented Nov 3, 2017 at 13:24

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

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.