3
\$\begingroup\$

I implemented a tree search with JavaScript.

It has below basic rules.

  • It will search over an array of objects which has recursive(like fractal)
  • If it found a according data in a leaf(the final depth one), it will return its ancestors, but without its siblings.(Example is below)

So here is an example.

Original one like this:

[  
   {  
      "value":"apple",
      "children":[  
         {  
            "value":"green-apple",
            "children":[  

            ]
         },
         {  
            "value":"red-apple",
            "children":[  
               {  
                  "value":"asia-red-apple",
                  "children":[  

                  ]
               },
               {  
                  "value":"europe-red-apple",
                  "children":[  

                  ]
               },
               {  
                  "value":"africa-red-apple",
                  "children":[  

                  ]
               }
            ]
         }
      ]
   },
   {  
      "value":"grape",
      "children":[  
         {  
            "value":"green-grape",
            "children":[  
               {  
                  "value":"asia-europe-green-grape",
                  "children":[  

                  ]
               },
               {  
                  "value":"asia-africa-green-grape",
                  "children":[  

                  ]
               },
               {  
                  "value":"africa-europe-green-grape",
                  "children":[  

                  ]
               }
            ]
         },
         {  
            "value":"red-grape",
            "children":[  
               {  
                  "value":"asia-red-grape",
                  "children":[  

                  ]
               },
               {  
                  "value":"europe-red-grape",
                  "children":[  

                  ]
               },
               {  
                  "value":"africa-red-grape",
                  "children":[  

                  ]
               }
            ]
         }
      ]
   }
]

Like I said before, it has children array element recursively. It user searches africa for instance, the result should be like below:

[  
   {  
      "value":"apple",
      "children":[  
         {  
            "value":"green-apple",
            "children":[  

            ]
         },
         {  
            "value":"red-apple",
            "children":[  
               {  
                  "value":"asia-red-apple",
                  "children":[  

                  ]
               },
               {  
                  "value":"europe-red-apple",
                  "children":[  

                  ]
               },
               {  
                  "value":"africa-red-apple",
                  "children":[  

                  ]
               }
            ]
         }
      ]
   },
   {  
      "value":"grape",
      "children":[  
         {  
            "value":"green-grape",
            "children":[  
               {  
                  "value":"asia-europe-green-grape",
                  "children":[  

                  ]
               },
               {  
                  "value":"asia-africa-green-grape",
                  "children":[  

                  ]
               },
               {  
                  "value":"africa-europe-green-grape",
                  "children":[  

                  ]
               }
            ]
         },
         {  
            "value":"red-grape",
            "children":[  
               {  
                  "value":"asia-red-grape",
                  "children":[  

                  ]
               },
               {  
                  "value":"europe-red-grape",
                  "children":[  

                  ]
               },
               {  
                  "value":"africa-red-grape",
                  "children":[  

                  ]
               }
            ]
         }
      ]
   }
]

0 and other parents value may not contain the keyword africa, but those will be returned since the final depth one(leaf) contains the keyword. That is the reason why I wrote reverse, the standard of search is on the final level.

The main logic implemented like here:

function recursiveSearch (coll, str, level) {
    if(!level) level = 0;
    console.log('level', level, ' coll size before job', coll.length);
    let removalValuesForThisLevel = [];
    let temp = [].concat(coll);
    temp.forEach(mem => {
        if(mem.children && mem.children.length > 0) {
            // recursive if has child
            let subList = recursiveSearch(mem.children, str, level + 1);
            mem.children = (subList) ? subList : null;
        } else {
            if((mem.value || '').indexOf(str) > -1) {
                // collect according values
                // console.log(mem.value,' is match for string :: ', str);
            } else {
                // collect removal values
                // console.log(mem.value,' is not match for string :: ', str);
                removalValuesForThisLevel.push(mem.value);
            }
        }
    });

    temp = temp.filter(mem => {
        return mem.children && (removalValuesForThisLevel.indexOf(mem.value) < 0);
    });

    console.log('level', level, ' coll size AFTER job', temp.length, JSON.stringify(temp))
    return (temp.length > 0) ? temp : (level === 0) ? [] : null;
}

It might work fine (in my thought), but I am worried about some parts.

  • If it handles tones of data(like billions).
  • Too many loops maybe?
  • Too many copy operations like Array.filter, Object.assign

You can advise me about good coding habbits and logics for JavaScript.

Here is a working fiddle.

\$\endgroup\$
  • \$\begingroup\$ "It might work fine (in my thought)" did you test it? And? \$\endgroup\$ – Mast May 26 at 14:36
  • 1
    \$\begingroup\$ @Mast I have ran this code with JSfiddle, attached to the post. However, I believe this code is not the best to achieve the goal. Sorry for confusing \$\endgroup\$ – Juneyoung Oh May 27 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.