Solved

jquery get unique values from array

Posted on 2011-02-28
3
973 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
ID: 35002953
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
ID: 35002963
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
ID: 35004775

// 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

785 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