I've been trying to apply the answers from How to remove all duplicates from an array of objects? on this but couldn't find a good way
My current solution: (this is when adding new iconsProps to the array, I'm trying to avoid adding one that already exists, unsuccesfully), I'm leaving the AnnotationIconsProps type as well, to help..
const idsExistsOnIconProps = iconsProps.some((id) => ids.includes(id.toString()));
const existingIndex = iconsProps.findIndex(
(iconProp) =>
iconProp.coordinates.left === element.offsetLeft &&
iconProp.coordinates.top === element.offsetTop &&
!idsExistsOnIconProps
);
if (existingIndex === -1) {
const newIconCoordinates: AnnotationIconProps[] = [
...iconsProps,
{
coordinates: {
top: element.offsetTop,
left: element.offsetLeft,
},
content: {
header: contentHeader,
text: contentText,
},
typeOfAnnotation: hasMultipleAnnotations
? "multiple"
: isSystemNotification
? "system"
: "single",
ids,
},
];
setIconProps(newIconCoordinates);
}
export type AnnotationIconProps = {
coordinates: {
top: number;
left: number;
};
content: {
header: string;
text: string;
};
typeOfAnnotation: AnnotationType;
ids: (string | number)[];
};
I was hoping to have a function in useEffect to check for duplicates, something like
useEffect(() => {
if (iconsProps.length > visibleAnnotations.length) {
// function to remove duplicates
}
}, [iconsProps]);