Solved

jquery get unique values from array

Posted on 2011-02-28
3
982 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

628 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