Cover Image for jQuery.unique() method
104 views

jQuery.unique() method

The jQuery.unique() function is not part of the core jQuery library but is provided by the jQuery Migrate plugin. The jQuery Migrate plugin is used to help developers migrate their code from older versions of jQuery to newer ones by providing compatibility functions.

Here’s the basic syntax of the jQuery.unique() method:

jQuery.unique(array)

Parameters:

  • array: The array from which duplicate elements should be removed.

Return Value:
The jQuery.unique() method returns a new array with duplicate elements removed.

Example:

<!DOCTYPE html>
<html>
<head>
    <title>jQuery unique() Method Example</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <script>
        $(document).ready(function() {
            // Sample array with duplicate elements
            var myArray = [1, 2, 2, 3, 4, 4, 5, 5, 6];

            // Use jQuery.unique() to remove duplicate elements
            var uniqueArray = jQuery.unique(myArray);

            // Display the result
            console.log(uniqueArray); // Output: [1, 2, 3, 4, 5, 6]
        });
    </script>
</body>
</html>

In this example, we have an array myArray with some duplicate elements. We use the jQuery.unique() method to create a new array uniqueArray with the duplicate elements removed.

Please note that as of jQuery version 3.0, the jQuery.unique() method was removed from the core library because it was considered not performant for large arrays. Instead, you can use native JavaScript methods like Array.from(new Set(array)) to achieve the same result of removing duplicates from an array without relying on the jQuery Migrate plugin.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS