Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8811543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:23:29+00:00 2026-06-14T03:23:29+00:00

I have a list of elements where I need to figure out the dependency.

  • 0

I have a list of elements where I need to figure out the dependency.

I have:

[{
  "a": ["b", "d"]
}, {
  "d": ["c", "e"]
}]

a is depending on b and d, and d on c and e. Is there a way to construct the dependencies in a clever way for this? The output should (could) be:

["b", "c", "e", "d", "a"]

/Kristian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:23:31+00:00Added an answer on June 14, 2026 at 3:23 am

    Assuming you wanted the list of recursive dependencies of an element, including the element itself, in any order:

    “for every dependency, add its dependencies to the dependency list” is clever enough?

    function recursiveDependencies (dependencies, element){
      var output = [element];
      for(i=0; i<output.length(); i++){
        dependencies[output[i]].forEach(function(x){
          if(output.indexOf(x)<0){ //prevent duplicates
            output.push(x)
          }
        })
      }
      return output;
    }
    

    If you want the element itself at the end rather than the beginning, you can fix that with output.push(output.shift())


    If you want your dependencies in such an order that every element precedes the elements that depend on it, then you’ll have to define how to handle circular dependencies. One way to handle that is detect a circular dependency and fail.

    If every dependency is needed by at most one element, then you can use the previous algorithm: simply read the output backwards (or reverse the array or use unshift instead of push (warning: performance drop))


    The dependencies form a graph, and you are looking for its topological sort. One (inefficent) way would be to order the nodes in depth-first order and reinsert them if they are reentered. You could use the Open stack to detect errors – if a child is reentered from its parent, then you have a circular dependency.

    A better solution would be to use the standard topological sort algorithm: While there are unsorted nodes, pick one that has no unresolved dependencies:

    function recursiveDependencies (dependencies, root){
      var nodes={};
      var nodeCount=0;
      var ready=[];
      var output=[];
    
      // build the graph
      function add(element){
         nodeCount++;
         nodes[element]={needs:[], neededBy:[], name: element};
         if(dependencies[element]){
           dependencies[element].forEach(function(dependency){
             if(!nodes[dependency]) add(dependency);
             nodes[element].needs.push(nodes[dependency]);
             nodes[dependency].neededBy.push(nodes[element]);
           });
         }
         if(!nodes[element].needs.length) ready.push(nodes[element]);
      }
    
      if(root){
        add(root)
      } else {
         for (element in dependencies){
           if(!nodes[element]) add(element);
        }
      }
    
    
      //sort the graph
      while(ready.length){
        var dependency = ready.pop();
        output.push(dependency.name);
        dependency.neededBy.forEach(function(element){
          element.needs = element.needs.filter(function(x){return x!=dependency})
          if(!element.needs.length) ready.push(element)
        })
      }
    
      //error-check
      if(output.length != nodeCount){
        throw "circular dependency detected"
      }
    
      return output;
    }
    

    fiddle: http://jsfiddle.net/Xq5dz/4/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out the best way to achieve this: I need to
I have a list of xml elements that i need to search for specific
I have two different list types. I need to remove the elements from list1
I have a large list [[1,.., ..],[2,...,...],[5,...,...],[1,...,...]] I need to remove all elements that
I have the following code. I need to create list separators before A elements,
There must be an easier way to do this. I need some text from
I have list elements like so: <li> <strong style=color: #BBB;>1</strong> <a href=>Matt</a> <b>4 days
I have a list of elements which needs to be displayed in a NSPopUpButton.
I have a list of elements which I would like to make sortable and
I have a list of elements that I want to split into individual lists

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.