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 :
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…