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

Determine if all object properties are equal

I am creating the boolean hasMultipleCoverageLines to determine if different coverageLineName values exist on the items within coverageLines. Is there a way to simplify this expression so i don’t have to explicitly check for each coverageLineName?

this.hasMultipleCoverageLines = !this.coverageLines.every((x) => x.coverageLineName === "Medical")
        && !this.coverageLines.every((x) => x.coverageLineName === "Dental")
        && !this.coverageLines.every((x) => x.coverageLineName === "Vision")
        && !this.coverageLines.every((x) => x.coverageLineName === "Life");

>Solution :

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

A set will enforce uniqueness. Put the names in a set, and see if the count > 1.

let names = this.coverageLines.map(x => x.coverageLineName);
let set = new Set(names);
this.hasMultipleCoverageLines = set.size > 1;

With this ["Dental", "Dental", "Dental"] will produce a set of size 1, but ["Dental", "Vision", "Dental"] will produce a set of size 2, and so on…

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