如何重构此数据以匹配其原始未拼合的形状?



我有以下数据模型:

新型号

[
        {
            name : 'Tony',
            id : 'Tony_id'
        },{
            name: 'Barry',
            id: 'Barry_id',
            parentId: 'Tony_id'
        },{
            name: 'George',
            id: 'George_id',
            parentId: 'Barry_id'
        },{
            name : 'Laura',
            id : 'Laura_id',
            parentId: 'Barry_id'
        },{
            name: 'Edward',
            id: 'Edward_id'
        },{
            name: 'Patrick',
            id: 'Patrick_id',
            parentId: 'Edward_id'
        }
   ]

所以结构是父母,孩子和孙子。最初模型如下所示,但我不得不将其展平:

旧型号

   [
        {
            name : 'Tony',
            id : 'Tony_id',
            children: [
                {
                    name: 'Barry',
                    id: 'Barry_id',
                    parentId: 'Tony_id',
                    children: [
                        {
                            name: 'George',
                            id: 'George_id',
                            parentId: 'Barry_id'
                        },{
                            name : 'Laura',
                            id : 'Laura_id',
                            parentId: 'Barry_id'
                        }
                    ]
                }
            ]
        },{
            name: 'Edward',
            id: 'Edward_id',
            children: [
                {
                    name: 'Patrick',
                    id: 'Patrick_id',
                    parentId: 'Edward_id'
                 }
            ]
        }
   ]

我已经失去了对旧模型的任何引用。

仅使用新模型,如何重组它以匹配我的旧模型,以取消展平它?

你可以用一个对象来构建一棵树,以保持父母和孩子的关系。

此建议也适用于未排序的数据。

var data = [{ name: 'Tony', id: 'Tony_id' }, { name: 'Barry', id: 'Barry_id', parentId: 'Tony_id' }, { name: 'George', id: 'George_id', parentId: 'Barry_id' }, { name: 'Laura', id: 'Laura_id', parentId: 'Barry_id' }, { name: 'Edward', id: 'Edward_id' }, { name: 'Patrick', id: 'Patrick_id', parentId: 'Edward_id' }],
    tree = function (data, root) {
        var r = [], o = {};
        data.forEach(function (a) {
            if (o[a.id] && o[a.id].children) {
                a.children = o[a.id] && o[a.id].children;
            }
            o[a.id] = a;
            if (a.parentId === root) {
                r.push(a);
            } else {
                o[a.parentId] = o[a.parentId] || {};
                o[a.parentId].children = o[a.parentId].children || [];
                o[a.parentId].children.push(a);
            }
        });
        return r;
    }(data, undefined);
console.log(tree);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

你可以递归地执行此操作:

var newData = [
        {
            name : 'Tony',
            id : 'Tony_id',
            children: [
                {
                    name: 'Barry',
                    id: 'Barry_id',
                    parentId: 'Tony_id',
                    children: [
                        {
                            name: 'George',
                            id: 'George_id',
                            parentId: 'Barry_id'
                        },{
                            name : 'Laura',
                            id : 'Laura_id',
                            parentId: 'Barry_id'
                        }
                    ]
                }
            ]
        },{
            name: 'Edward',
            id: 'Edward_id',
            children: [
                {
                    name: 'Patrick',
                    id: 'Patrick_id',
                    parentId: 'Edward_id'
                 }
            ]
        }
   ];
function flat(datas, flatten) {
    datas.forEach(el => {
        flatten.push(el);
        if (el.children) {
            flat(el.children, flatten);
            delete el.children;
        }
    });
}
var flatten = [];
flat(newData, flatten);
console.log(flatten);

编辑:要做相反的事情:

var data = [{ name: 'Tony', id: 'Tony_id' }, { name: 'Barry', id: 'Barry_id', parentId: 'Tony_id' }, { name: 'George', id: 'George_id', parentId: 'Barry_id' }, { name: 'Laura', id: 'Laura_id', parentId: 'Barry_id' }, { name: 'Edward', id: 'Edward_id' }, { name: 'Patrick', id: 'Patrick_id', parentId: 'Edward_id' }];
function ToTree(arrayItems) {
    var map = {}, roots = [];
    arrayItems.forEach((item, i) => {
        map[item.id] = i;
        item.children = [];
    });
    arrayItems.forEach(node => {
        if (node.parentId && arrayItems[map[node.parentId]]) {
            arrayItems[map[node.parentId]].children.push(node);
        } else {
            roots.push(node);
        }
    });
    return roots;    
}
var myTree = ToTree(data);
console.log(myTree);

(为了好玩),相反:

let oldData = [
  {name : 'Tony', id : 'Tony_id'},
  {name: 'Barry', id: 'Barry_id', parentId: 'Tony_id'},
  {name: 'George', id: 'George_id', parentId: 'Barry_id'},
  {name : 'Laura', id : 'Laura_id', parentId: 'Barry_id'},
  {name: 'Edward', id: 'Edward_id'},
  {name: 'Patrick', id: 'Patrick_id', parentId: 'Edward_id'}
]
// First we need a Key-Data object with :
// Key   : person's id
// Value : person's data
let dwKeys = {}
oldData.forEach( d => dwKeys[d.id] = d) ;
// The same objects with children
let dwChildren = {} ;
// Then we can know who's who
for(let uid in dwKeys)
{
    
  // We take the person's data
  let udata = dwKeys[uid] ;
  
  // we take the parent (if any)
  
  let parentId = udata.parentId ;
  
  // No parent ? OK => root person
  
  if ( ! parentId ) {
    dwChildren[uid] = udata ;
    continue ; 
  } 
    
  // Now, we add the child to their parent
  
  // Do the record if needed
  
  if ( undefined === dwChildren[parentId] ){ 
    dwChildren[parentId] = dwKeys[parentId] ;
  }
  
  // Do the children field if needed
  
  if (undefined === dwChildren[parentId]['children'] ){
    dwChildren[parentId]['children'] = [];
  }
  
  // Add this child
  
  delete udata.parentId ;
  dwChildren[parentId]['children'].push(udata);
  
}
// Hey no, it doesn't work:
// dwChildren = dwChildren.values() ;
// So:
let awChildren = [] ; // for "Array With Children"
for (let uid in dwChildren ) { awChildren.push(dwChildren[uid]) }
console.log(awChildren);
.as-console-wrapper { max-height: 100% !important; top: 0; }

最新更新