Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

More efficient way possible?

I have this piece of code, however I think it can be written more efficient.

this.selectedDub = this.items.filter(item =>
      item.fields.dub_Key.value === value);

this.selectedDubContent = this.selectedDub[0].fields as Dub;
this.selectedDubRendering = this.selectedDub[0] as TypedComponentRendering<Dub>;

Can anyone point me in the right direction?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

this.selectedDubRendering = this.items.find(item =>
      item.fields.dub_Key.value === value) as TypedComponentRendering<Dub>;

this.selectedDubContent = this.selectedDubRendering?.fields ?? undefined;

You could check out the TypeScript find method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

The find() method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading