Function to remove duplicates in array

  /**

        * Removes duplicate objects from an array.

        * @param {Array} arr – The array containing objects.

        * @param {string} [prop] – The property to check for duplicates.

        * @returns {Array} – The array with duplicate objects removed.

        */

        removeDuplicateObjects(arr, prop) {

            const seen = new Set();

            return arr.filter(item => {

                const key = prop ? item[prop] : JSON.stringify(item);

                if (!seen.has(key)) {

                    seen.add(key);

                    return true;

                }

                return false;

            });

        },

Leave a comment

Your email address will not be published. Required fields are marked *