Solved

array in recurssive function

Posted on 2014-10-15
3
72 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
Comment Utility
declare outside (yes you get duplicate)
after it's done remove duplicate from the array(s)
0
 
LVL 5

Author Comment

by:Isaac
Comment Utility
How would I remove the duplicates?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

762 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

12 Experts available now in Live!

Get 1:1 Help Now