Flattening a nested array of asymetrical depth

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Flattening a nested array of asymetrical depth

Tucsonjhall
I'm working with nested arrays of divisions in sports and need to flatten a
nested array of divisions and subdivisions to find all instances of
"division." I've been stumped how to recurse that structure so that I'm sure
I traverse all branches of the array without arbitrarily setting a limit of
how deep I will go searching for children. Suggestions on a script that
should flatten any array of objects with children?

Thanks!



--
Sent from: http://apache-flex-users.2333346.n4.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Flattening a nested array of asymetrical depth

Paul Stearns
Recursion is most likely the answer, however, if you could define the structure a little better it would be easier to help.

Showing a little code that defines the structure would be best.

Paul R. Stearns
Advanced Consulting Enterprises, Inc.

15280 NW 79th Ct.
Suite 250
Miami Lakes, Fl 33016

Voice: (305)623-0360 x107
Fax: (305)623-4588

----------------------------------------
From: Tucsonjhall <[hidden email]>
Sent: 10/10/19 11:05 AM
To: [hidden email]
Subject: Flattening a nested array of asymetrical depth
I'm working with nested arrays of divisions in sports and need to flatten a
nested array of divisions and subdivisions to find all instances of
"division." I've been stumped how to recurse that structure so that I'm sure
I traverse all branches of the array without arbitrarily setting a limit of
how deep I will go searching for children. Suggestions on a script that
should flatten any array of objects with children?

Thanks!

--
Sent from: http://apache-flex-users.2333346.n4.nabble.com/


Reply | Threaded
Open this post in threaded view
|

Re: Flattening a nested array of asymetrical depth

kamcknig
In reply to this post by Tucsonjhall
Done in JS and def could be optimized. I have no idea on your data structure
or what you want the final structure to be.

https://pastebin.com/p0j2uTnp

Kyle



--
Sent from: http://apache-flex-users.2333346.n4.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Flattening a nested array of asymetrical depth

Tucsonjhall
In reply to this post by Paul Stearns
Hope the formatting of this comes out OK in post. Here's a typical structure.

Softball Minors
        East
        West
Softball Majors
        National
                East
                West
        American
                East
                West
        Independent
                East
                        Blue
                        Green
                West
                        Orange
                        Yellow



--
Sent from: http://apache-flex-users.2333346.n4.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Flattening a nested array of asymetrical depth

Tucsonjhall
In reply to this post by kamcknig
I appreciate it and I know I should be able to translate JS into strongly
typed AS3 but I often crash and burn because of the syntax differences. On
my todo list ;-)



--
Sent from: http://apache-flex-users.2333346.n4.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Flattening a nested array of asymetrical depth

kamcknig
So my example will still work though because your 'nodes' don't have
consistent naming, you'll probably have to add some data to the levels that
you want to extract into the flattened array and then where I'm checking for
"division" you would check for the new data that indicates it's at the level
you want to extract.

As for JS to AS, technically I did it in TypeScript. But it should be
directly portable aside from the types being used (just translate to as
types) and then the Array.isArray is specific to JS



--
Sent from: http://apache-flex-users.2333346.n4.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Flattening a nested array of asymetrical depth

Alex Harui-2
In reply to this post by Tucsonjhall
The Apache Royale Compiler will transpile your AS3 to JS for you.  That's the main point of Royale.  Keep your code strongly-typed so the tool chain can catch more errors sooner so there are fewer times you crash and burn.

-Alex

´╗┐On 10/10/19, 9:25 AM, "Tucsonjhall" <[hidden email]> wrote:

    I appreciate it and I know I should be able to translate JS into strongly
    typed AS3 but I often crash and burn because of the syntax differences. On
    my todo list ;-)
   
   
   
    --
    Sent from: https://nam04.safelinks.protection.outlook.com/?url=http%3A%2F%2Fapache-flex-users.2333346.n4.nabble.com%2F&amp;data=02%7C01%7Caharui%40adobe.com%7Cce2af09d0c864b13038708d74d9e73b5%7Cfa7b1b5a7b34438794aed2c178decee1%7C0%7C1%7C637063215255315096&amp;sdata=yJIVwXlSdolj7rzqWMmOCmSXCqIMhxdgGkEEv45Pbbk%3D&amp;reserved=0
   

Reply | Threaded
Open this post in threaded view
|

Re: Flattening a nested array of asymetrical depth

Tucsonjhall
Reply | Threaded
Open this post in threaded view
|

Re: Flattening a nested array of asymetrical depth

Tucsonjhall
In reply to this post by kamcknig
Will give it a shot. Thanks much.



--
Sent from: http://apache-flex-users.2333346.n4.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Flattening a nested array of asymetrical depth

Tucsonjhall
This post was updated on .
Well as I suspected, my attempt to translate such symbols as => didn't go
well given my limited knowledge of JS. One important thing I left out. There
IS consistency in the naming that I  forgot to mention. All the objects have
children in the form of an array named children. Does that make it any
easier to get me on the right track to recurse through such a structure in AS3?


Softball Minors (object)
        children (array)
                        East (object)
                        West (object)
Softball Majors (object)
        children (array)
                National (object)
                        children (array)
                                East (object)
                                West (object)
                American (object)
                        children (array)
                                East (object)
                                West (object)
                Independent (object)
                        children (array)
                                East
                                        children (array)
                                                Blue
                                                Green
                                West
                                        children (array)
                                                Orange
                                                Yellow

Obviously this implies that I don't yet fully understand recursion in AS3. If anyone has an article I can read to better comprehend that doesn't simply use the typical Fibonacci examples, I open to learning.
--
Sent from: http://apache-flex-users.2333346.n4.nabble.com/