Solved

array in recurssive function

Posted on 2014-10-15
3
76 Views
Last Modified: 2014-10-17
I'm trying to modify a function and use an array.  It's a private function that seems to be in a closure.
The problem I'm having is that when _GetDataTable makes a call to itself, the array resets (understandable).
When I declare the array outside of the function, I get duplicate values of the same data.  Any ideas how to fix?
Thanks!       

      
function _GetDataTable() //private function
	{ 
		var retVal = "";
		var chkVal = "";

		colHeader = new Array();
		dataTableData = new Array();
		
		//base case:
		if(arguments.length == 1)
		{
			var theCase;
			if( typeof arguments[0][0] == "string")
			{
				if(arguments[0][1] && __MatchCase(arguments[0][1])) 
				{
					
					if(arguments[0].length == 4)
					{	
						colHeader.push(arguments[0][2]);
						dataTableData.push(arguments[0][0]);
					}
					else if(arguments[0].length == 5) 
					{							
						colHeader.push(arguments[0][3]);
						dataTableData.push(arguments[0][0]);

					}
				}
			}
			else if((arguments[0].length == 4) || (arguments[0][2] && __MatchCase(arguments[0][2]))) 
			{
					colHeader.push(arguments[0][2]);
					dataTableData.push(arguments[0][0].getProperty(arguments[0][1]));	
			}
		}
		else
		{
			for(var i = 0; i < arguments.length; ++i)
			{
						retVal += _GetDataTable(arguments[i]);		
			}
		}	

		return retVal;
	};

Open in new window

0
Comment
Question by:Isaac
  • 2
3 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40383190
declare outside (yes you get duplicate)
after it's done remove duplicate from the array(s)
0
 
LVL 5

Author Comment

by:Isaac
ID: 40383241
How would I remove the duplicates?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40383250
var found = {};
var clean = [];
for(var i=0;i<myarray.length;i++) {
   var notfound = !found[ myarray[i] ];
   if(notfound) {
      clean.push(myarray[i]);
      found[myarray[i]] = true;
   }
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
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…
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now