Solved

jquery get unique values from array

Posted on 2011-02-28
3
979 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

751 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