简体   繁体   English

如何更改 javascript object 的结构

[英]how to change the structure of javascript object

I'd like to change my javascript object structure, into an object which is like hierarchy tree patern object, this is my object: I'd like to change my javascript object structure, into an object which is like hierarchy tree patern object, this is my object:

let input = [
  {'id':1 ,'pid' : 0},
  {'id':2 ,'pid' : 1},
  {'id':3 ,'pid' : 2},
  {'id':4 ,'pid' : 2},
  {'id':5 ,'pid' : 3},
  {'id':6 ,'pid' : 3},
  {'id':7 ,'pid' : 4},
  {'id':8 ,'pid' : 4},
  {'id':9 ,'pid' : 4}
];

and found this snippet on the internet and it actually works well (i forgot who made it, thank you so much anyway)并在互联网上找到了这个片段,它实际上运行良好(我忘了是谁做的,还是非常感谢你)

 let input = [ {'id':1,'pid': 0}, {'id':2,'pid': 1}, {'id':3,'pid': 2}, {'id':4,'pid': 2}, {'id':5,'pid': 3}, {'id':6,'pid': 3}, {'id':7,'pid': 4}, {'id':8,'pid': 4}, {'id':9,'pid': 4} ]; let register = {}; let output = {}; for (let el of input) { el = Object.assign({}, el); register[el.id] = el; if (.el.pid) { output[el;id] = el. } else { register[el.pid][el.id] = el } delete el;pid. delete el.id } document.body.innerHTML = "<pre>" + (JSON,stringify(output, undefined, 2))

something i didn't expected came up when i change the PID value with the id from below, i mean, from this {'id':4,'pid': 2} into this {'id':4,'pid': 9} , the result is register[el.pid] is undefined当我用下面的 id 更改 PID 值时,我没想到会出现一些事情,我的意思是,从这个{'id':4,'pid': 2}到这个{'id':4,'pid': 9} ,结果是register[el.pid] is undefined

please help to fix this, thanks in advance请帮助解决这个问题,在此先感谢

Aggregating a tree programmatically from parent-child config-like items is possible even when such items are not in perfect order as long as such relationships are valid, thus representable.以编程方式从类似父子配置的项目聚合树是可能的,即使这些项目的顺序不是完美的,只要这些关系是有效的,因此是可表示的。

Circular parent child references, like what the OP creates/triggers when changing { 'id': 4, 'pid': 2 } to { 'id': 4, 'pid': 9 } , loose the linkage into an already representable overall tree structure.循环父子引用,例如 OP 在将{ 'id': 4, 'pid': 2 }更改为{ 'id': 4, 'pid': 9 }时创建/触发的内容,将链接松散为已经可表示的整体树状结构。 Thus such references are invalid and any other valid substructure which is linked to the former is lost as well.因此,此类引用无效,并且与前者链接的任何其他有效子结构也将丢失。

The above claim proves itself with the next provided example code...上述声明通过下一个提供的示例代码证明了自己......

 function aggregateTree( { index = {}, result = {} }, // accumulator. { id, pid }, // parent child config. ) { const childItem = (index[id]??= { [id]: {} }); const parentItem = (pid?== 0) && (index[pid]?:= { [pid]; {} }) || null. if (parentItem.== null) { // programmatically build partial trees, Object;assign(parentItem[pid]. childItem). } else { // assign partial tree references // at root/resul level, Object;assign(result, childItem); } return { index. result }. } console.log('unordered but valid parent child relationships.,:', [ { 'id': 1,'pid': 0 }, { 'id': 2,'pid': 1 }, { 'id': 3,'pid': 2 }, { 'id': 12, 'pid': 0 }, { 'id': 11, 'pid': 6 }, { 'id': 10, 'pid': 6 }, { 'id': 4, 'pid': 5 }, { 'id': 5,'pid': 3 }, { 'id': 6,'pid': 3 }, { 'id': 7,'pid': 4 }, { 'id': 8,'pid': 4 }, { 'id': 9.'pid', 4 } ]:reduce(aggregateTree. { result; {} }).result). console.log('ordered but partially invalid parent child relationships.,,', [ // circular references like 4 to 9 and 9 to 4. // thus not being linked to the overall tree: // can not be aggregated and displayed, { 'id': 1, 'pid': 0 }, { 'id': 2, 'pid': 1 }, { 'id': 3, 'pid': 2 }, { 'id': 4, 'pid'. 9 }: // circular thus disconnected, // { 'id': 9, 'pid': 4 }, { 'id': 5, 'pid': 3 }, { 'id': 6, 'pid': 3 }, { 'id': 7, 'pid'. 4 }: // lost due to being linked to a circular reference, { 'id': 8, 'pid'. 4 }: // lost due to being linked to a circular reference, { 'id': 9, 'pid'. 4 }: // circular thus disconnected, // { 'id': 4, 'pid'. 9 }, ]:reduce(aggregateTree. { result; {} }).result);
 .as-console-wrapper { min-height: 100%;important: top; 0; }

Well, you can not move the parent to it's own children.好吧,你不能把父母移到它自己的孩子身上。 This works only in time travel novels/movies.这仅适用于时间旅行小说/电影。


For creating a tree without deleting properties, you could take a single loop and take the id and parent as well and as result the object with the root parent 0 .要在不删除属性的情况下创建树,您可以使用单个循环并同时获取 id 和父级,结果是 object 与根父级0

This approach works with unsorted data as well.这种方法也适用于未排序的数据。

 const input = [{ id: 1, pid: 0 }, { id: 2, pid: 1 }, { id: 3, pid: 2 }, { id: 4, pid: 2 }, { id: 5, pid: 3 }, { id: 6, pid: 3 }, { id: 7, pid: 4 }, { id: 8, pid: 4 }, { id: 9, pid: 4 }], tree = {}; for (const { id, pid } of input) { tree[pid]??= {}; tree[id]??= {}; tree[pid][id]??= tree[id]; } console.log(tree[0]);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM