Solved

array in recurssive function

Posted on 2014-10-15
3
83 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
[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: 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

740 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