Is there a way to flatten a nested array? For example, is there a way to turn this array:
['frank', ['bob', 'lisa'], ['jill', ['tom', 'sally']]]
into this array:
['frank', 'bob', 'lisa', 'jill', 'tom', 'sally']
Steve In LA
Is there a way to flatten a nested array? For example, is there a way to turn this array:
['frank', ['bob', 'lisa'], ['jill', ['tom', 'sally']]]
into this array:
['frank', 'bob', 'lisa', 'jill', 'tom', 'sally']
Steve In LA
I think the only option is that you loop through it…
I thought so, but I was having trouble using a recursive method to step through the unpredictable number of branches. The recursive method’s loop counter kept getting messed up. The solution was to push each recursion’s incremented counter to an array stored in a global variable and pop the last value off that array to get the current counter value.
Why or how do you have data like that? It looks like something like XML is more suitable to store that sort of structures.
I am working on a way to add a “kit” service to a quote, where a “kit” service is one that is made up of other “component” services. The component services may themselves be kits, which may contain components that are also kits, and so on.
If a kit service is selected to add to a quote, additional line items must be added automatically for the kit’s components. The problem I had was being able to add the line items in the order of a “flattened” version of the whole kit structure. I was able to build an object that stored each component record that was structured properly, but once built, I could not figure out how to incrementally walk through the object, creating line items with an appropriate line number value.
I was trying to loop through the object using a recursive method with a variable counter in the method, but that counter would get reset at the start of each recursion. The solution was to use a separate array stored in a temporary global variable that held each recursion’s counter and removed it when the recursion finished. At any given time, the last value of the array is the current recursion’s counter.
Here is an example of a kit service’s object structure:
vKitObject
vKitObject.components
vKitObject.components.component1
vKitObject.components.component2
vKitObject.components.component2.components.component1
vKitObject.components.component2.components.component2
vKitObject.components.component2.components.component2.components.component1
vKitObject.components.component2.components.component2.components.component2
vKitObject.components.component2.components.component2.components.component3
vKitObject.components.component2.components.component3
vKitObject.components.component3
This method receives the built object and sets things up to add a “line_num” property to each component:
vgLineNum = 1; //or starting line number
vgCounterArray = new Array();
vgCounterArray.push( 1 );
add_line_nums_to_object_sub( arguments[ 0 ] )
Here is the recursive method that walks through the object. Now that I have this part figured out, this is probabaly where the quote line item will be created:
var vCurrentObject = arguments[ 0 ];
var okToContinue = true;
while ( okToContinue )
{
if ( vCurrentObject.components != null )
{
var vCounter = vgCounterArray.pop();
if ( vCurrentObject.components[ "component" + vCounter ] != null )
{
var vCurrentObjectComponent = vCurrentObject.components[ "component" + vCounter ];
vCurrentObjectComponent.line_num = vgLineNum;
vgLineNum += 1;
vgCounterArray.push( vCounter + 1 );
if ( vCurrentObjectComponent.components != null )
{
vgCounterArray.push( 1 );
add_line_nums_to_object_sub( vCurrentObjectComponent ); //calling itself here
}
}
else
{
okToContinue = false;
}
}
else
{
okToContinue = false;
}
}
It works and makes sense to me. Whether it works and makes sense to anyone else is yet to be seen.