How To Get Unique Values From Array in jQuery Tutorial

Reading Time: 3 minutes
1,297 Views

Inside this article we will see the concept i.e How To Get Unique Values From Array in jQuery. Article contains the classified information about getting unique values from array in jquery.

Selecting values from array in jquery is a very basic concept but if your array contains duplicates then you need some logic to filter out the unique values from it.

If you are looking for a solution to select the distinct values from javascript array then this article will help you a lot for this. We will use filter() method of jQuery.

Learn More –

Let’s get started.

Application Programming

Create a file index.html inside your project folder.

Open index.html and write this following code into it.

<!DOCTYPE html>
<html>
<head>
   <title>How To Get Unique Values From Array in jQuery Tutorial</title>
   <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
</head>
<body>
  
<script type="text/javascript">

    var employees = ["Sanjay","Siddharth","Dhananjay","Sanjay"];
  
    var uniqueEmployees = employees.filter(function(item, i, emp) {
        return i == emp.indexOf(item);
    });
  
    console.log(uniqueEmployees);
</script>
  
</body>
</html>

Concept

Filter Unique values from array.

var uniqueEmployees = employees.filter(function(item, i, emp) {
    return i == emp.indexOf(item);
});

Application Testing

Now,

Open project into browser.

URL: http://localhost/index.html

We hope this article helped you to learn How To Get Unique Values From Array in jQuery Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.