简体   繁体   English

更改与父子相关的嵌套对象的ID

[英]change parent child related ids of nested objects

I have following array of objects 我有以下对象的数组

{
  id: 'a',
  parent_id: 'root'
}

    {
        id: 'a1',
        parent_id: 'a'
    }

        {
          id: 'a11',
          parent_id: 'a1'
        }
        {
          id: 'a12',
          parent_id: 'a1'
        }
        {
          id: 'a13',
          parent_id: 'a1'
        }
        .
        .
        .
        .
        .

    {
        id: 'a2',
        parent_id: 'a'
    }

        {
          id: 'a21',
          parent_id: 'a2'
        }
        {
          id: 'a22',
          parent_id: 'a2'
        }
        {
          id: 'a23',
          parent_id: 'a2'
        }
        .
        .
        .
        .
        .

I have one root element with the id a and it has multiple children ( children are saved with parent_id property) 我有一个根ID为a根元素,它有多parent_id (子级使用parent_id属性保存)

I'm saving in the same format in mongodb. 我以相同的格式保存在mongodb中。 I want to share same records with another user and 我想与其他用户共享相同的记录,并且

My data model needs these Ids to be unique, so I just want to copy these array of objects into a separate var and changes these id's to random strings maintaining parent and child relationship. 我的数据模型需要这些ID是唯一的,因此我只想将这些对象数组复制到一个单独的var中,然后将这些id更改为保持父子关系的随机字符串。

I can generate random string from one of the libraries using Random.id() now I don't know how to loop through all the children and change the id and parent_id property, any help appreciated. 我现在可以使用Random.id()从其中一个库中生成随机字符串,现在我还不知道如何遍历所有子级并更改idparent_id属性,因此不胜感激。

Thanks. 谢谢。

I figure out a solution, you can dig into the code... 我想出了一个解决方案,您可以深入研究代码...

The code find the max id, and then, walk the tree, changing the ids, if you need a alfanumeric id, you can make some kind of random id and use the same walking function. 代码找到最大ID,然后遍历树,更改ID,如果需要字母数字ID,则可以制作某种随机ID并使用相同的Walk函数。

 var data = [{id: 10, parent_id:null}, {id: 20, parent_id:10}]; function findTopLevel() { return data.find(function(val, i, arr) { return val.parent_id == null || val.parent_id == undefined }); }; function findChildren(parent) { return data.filter(function(val, i, arr) { return val.parent_id == parent.id }); }; function findMaxId(node, max) { max = Math.max(node.id, max || -1); var children = findChildren(node); for (var i = 0; i < children.length; i++) { max = findMaxId(children[i], max); } return max; }; function walk(parent) { var parentId = ++maxId; var children = findChildren(parent); parent.id = parentId; for (var i in children) { children[i].parent_id = parentId; walk(children[i]); } }; var topLevel = findTopLevel(); var maxId = findMaxId(topLevel); walk(topLevel); console.log(maxId, data) 

I guess this might be what you wanted; 我想这可能就是您想要的;

 function makeUniqueId(){ var id = "", charList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", numList = "0123456789"; for( var i=0; i < 2; i++ ) id += charList.charAt(Math.floor(Math.random() * charList.length)); for( var j=0; j < 3; j++ ) id += numList.charAt(Math.floor(Math.random() * 10)); return !~uil.indexOf(id) ? (uil.push(id),id) : makeId(); } var uil = [], // unique id list data = [ { id: 'a', parent_id: 'root' },{ id: 'a1', parent_id: 'a' },{ id: 'a11', parent_id: 'a1' },{ id: 'a12', parent_id: 'a1' },{ id: 'a13', parent_id: 'a1' },{ id: 'a2', parent_id: 'a' },{ id: 'a21', parent_id: 'a2' },{ id: 'a22', parent_id: 'a2' },{ id: 'a23', parent_id: 'a2' }]; data.forEach((e,i,a) => {var puid = makeUniqueId(); a.forEach(f => f.parent_id == e.id && (f.parent_id = puid)); e.id = puid; }); document.write("<pre>" + JSON.stringify(data,null,2) + "</pre>"); 

Everytime you run the code you will get different unique values for the id and parent_id fields. 每次运行代码时, idparent_id字段都会获得不同的唯一值。 If you want to get this one get nested then it's a total different story. 如果您想让这个嵌套,那就完全不同了。

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

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