Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: crummy on Oct 13th, 2013  |  syntax: JavaScript  |  size: 0.98 KB  |  views: 54  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function (tree) {
  2.                     // http://programmers.stackexchange.com/questions/214227/
  3.                     var stack = [],
  4.                         replies = tree.replies.slice(1, tree.replies.length),
  5.                         root = tree.replies[0];
  6.                     root.children = [];
  7.                     stack.push(root);
  8.                     replies.forEach(function (reply) {
  9.                         var delta = reply.depth - stack.length;
  10.                         reply.children = [];
  11.                         if (delta > 0) {
  12.                             assert(delta === 1);
  13.                             stack.push(stack.front().children.front());
  14.                         }
  15.                         while (delta < 0) {
  16.                             stack.pop();
  17.                             delta++;
  18.                         }
  19.                         stack.front().children.push(reply);
  20.                     });
  21.                     this.rootReply = root;
  22.                 },
clone this paste RAW Paste Data