Removing duplicate values from multi dimensional array using JavaScript

Advertisement
Post Pic

Introduction:

This article will helps you in removing any duplicate values from Multi Dimensional Array using simple JavaScript. Here I have used a simple logic, hope everyone can understand. Even, this article will help beginners in learn some simple tricks in JavaScript.

JavaScript Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!-- Author: K.Vivekanand (http://www.developersnippets.com) -->
<!-- Contact: vivek@developersnippets.com -->
<script type="text/javascript">
var testArray = [
  [3, "Adobe"],
  [3, "Adobe India"],  
  [5, "Photoshop"],
  [4, "Flash"],
  [1, "Bridge"],
  [4, "Flash CS3"],
  [3, "Adobe Gumbo"]
];
 
var tempArray = new Array();
tempArray[0]=testArray[0];
	for(var i=0;i<testArray.length;i++)
	{
		var flag = true;
		for(var j=0;j<tempArray.length;j++)
		{
			if(tempArray[j][0]==testArray[i][0])
			{
				flag = false;
			}
		}//for loop
		if(flag==true)
		tempArray.push(testArray[i]);
	}//for loop
 
document.write(tempArray);
</script>

I am removing values depending on first element in the array that is 3,3,5,4,1,4,3. If you look at this one 3 and 4 are repeated twice. After applying simple logic, the output would be:

OutPut:

3,Adobe,5,Photoshop,4,Flash,1,Bridge

If you want to same concept for one dimensional array that is simple array, then you can view this article for reference – Remove duplicates from an array using JavaScript

Related Entries...

You Might Like:

Advertisement

Please let us know your feedback about the article through the comments section below, we want to hear from you about our website. And your comment / advice may help us to serve you better in upcoming articles.

Thank You!
Vivekanand [Founder of DeveloperSnippets]

2 Responses

This is really cool and easy ……. havent thought that must be so easy :)

05.08.09

Works great, perfect for what i was looking for! Thanks!

Leave Your Response

* Name, Email, Comment are Required

Write For Us

Online Sponsors

Start Foreign Exchange Trading today with the Forex Affiliate Program and also you can have access Online Forex News to know what happens every single moment.


Forex Floor


Switch to our mobile site