Solved

jquery get unique values from array

Posted on 2011-02-28
3
968 Views
Last Modified: 2012-05-11
So lets say i have an array

var arr1 = ["jeff","bob","jeff","bob","joe","jeff","jeff"];

how can in jquery(preferrably) or javascript get another array arr2 with only the unique values by order of first occurence so

arr2 = ["jeff","bob","joe"];
0
Comment
Question by:Brant Snow
  • 2
3 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Using this :

var arr1 = ["jeff","bob","jeff","bob","joe","jeff","jeff"];
		var arr2 = [];
		for(var i=0;i<arr1.length;i++) {
		   var found = false;
		   for(var j=0;j<arr2.length;j++) {
			  if(arr1[i] == arr2[j]) {
				   found = true;
				   break;
			  }
		   }
		   if(!found) arr2.push(arr1[i]);
		}

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	window.onload = function() {
		var arr1 = ["jeff","bob","jeff","bob","joe","jeff","jeff"];
		var arr2 = [];
		for(var i=0;i<arr1.length;i++) {
		   var found = false;
		   for(var j=0;j<arr2.length;j++) {
			  if(arr1[i] == arr2[j]) {
				   found = true;
				   break;
			  }
		   }
		   if(!found) arr2.push(arr1[i]);
		}
		
		// checking 
		for(var i=0;i<arr2.length;i++) {
			alert(arr2[i]);
		}
	}
</script>
</head>
<body>
</body>
</html>

Open in new window

0
 
LVL 20

Expert Comment

by:Proculopsis
Comment Utility

// This is quite a neat solution:

Array.prototype.distinct = function() {
  var unique = new Object();
  var index = -1;
  while ( ++index != this.length ) unique[this[index]] = null;
  var result = new Array();
  for ( var entry in unique ) result.push( entry );
  return result;
};


var arr1 = ["jeff","bob","jeff","bob","joe","jeff","jeff"];

var arr2 = arr1.distinct();

alert( arr2 );
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now