v1
- by dimoff66 10/26/202000
Setup HTML - click to add setup HTML
disable setup JavaScript
Setup JavaScript
const arr = [];
var len = 1000;
function init() {
for (var i = 0; i < len; i++) {
    arr.push({
        id: i+1,
        name: i+" ...",
        parent: i
    });
}}

init();
delete caserun single casemove downdrag and drop case


ready



function pack( data ){
  const childs = id => 
    data.filter( item => item.parent === id )
      .map( 
        ({id,name}) => ({id,name, children: childs(id)}) 
      ).map(
        ({id,name,children}) => children.length ? {id,name, children} : { id, name }
      );
  return childs(0);
}

const res = pack(arr)
delete caserun single casemove upmove downdrag and drop case


ready



function pack( arr ) {
  const map = Object.assign({} , ...arr.map(v => 
    ({ [v.id]: Object.assign(v, { children: [] }) })
  ))

  const tree = Object.values(map).filter(v => 
   !(v.parent && map[v.parent].children.push(v))
  )
  
  return tree
}

const res = pack(arr)
delete caserun single casemove updrag and drop case


ready



function makeTree(array, parent) {
  var tree = {};
  parent = typeof parent !== 'undefined' ? parent : {id: 0};

  var childrenArr = array.filter(function(child) {
    return child.parent == parent.id;
  });

  if (childrenArr.length > 0) {
    var childrenObj = {};
    childrenArr.forEach(function(child) {
      childrenObj[child.id] = child;
    });
    if (parent.id == 0) {
      tree = childrenObj;
    } else {
      parent.children = childrenObj;
    }
    childrenArr.forEach(function(child) {
      makeTree(array, child);
    })
  }

  return tree;
};

const res = makeTree(arr);
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN