简体   繁体   中英

Transform Array of level+data to List tree structure

I am writing a program that takes in an array and then makes a ul tree type structure that is collapsible. I am doing this in java script. I get strings in an array like this:

{"1, Dog, true"},
{"2, German Shepard, true"},
{"3, Spotty, true"},
{"2, Jack Russel, true"},
{"3, Blacky, true"},
{"1, Cats, true"},
{"1, Horses, true"}

This should produce

  • Dog
    • German Shepard
      • Sptty
    • Jack Russel
      • Blacky
  • Cats
  • Horses

I have tried making the array multi dimensional and just iterating through that, but there is sometimes 4 levels and a possibility of it becoming 5. True will make the list item green while false will make the list item red. I split the array by "," and if the number is bigger than the previous one I make a new ul, if the number is smaller than I close a ul, but I have trouble when the previous number is smaller by >2. Some assistance will be great thanks.

So transform this to JSON, like

{
   "Dog" : {
     "isGood":true,
     "German Shepherd":{

     }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM