Solved

array in recurssive function

Posted on 2014-10-15
3
82 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
If condition on Html with Asp 11 27
Add shadow behind div 5 25
Google Analytics setup 5 50
Help with Javascript to round decimal points to 2 13 15
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

856 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