【问题标题】:change parent child related ids of nested objects更改嵌套对象的父子相关ID
【发布时间】:2016-05-16 19:59:27
【问题描述】:

我有以下对象数组

{
  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'
        }
        .
        .
        .
        .
        .

我有一个 id 为 a 的根元素,它有多个子元素(子元素使用 parent_id 属性保存)

我在 mongodb 中以相同的格式保存。我想与另一个用户共享相同的记录,并且

我的数据模型需要这些 Id 是唯一的,所以我只想将这些对象数组复制到一个单独的 var 中,并将这些 id 更改为保持父子关系的随机字符串。

我可以使用 Random.id() 从其中一个库中生成随机字符串,现在我不知道如何循环遍历所有子项并更改 idparent_id 属性,感谢任何帮助。

谢谢。

【问题讨论】:

  • ID 已经是唯一的了.. 有什么问题。?是否要嵌套对象结构?
  • @Redu 我想将同一组卡片保存到不同的用户,所以我必须将同一组 _id 保存到同一个集合。这就是为什么在我将同一组集合保存给不同的用户之前,我想更改 ID。

标签: javascript jquery arrays loops object


【解决方案1】:

我猜这可能是你想要的;

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>");

每次运行代码时,idparent_id 字段都会获得不同的唯一值。如果你想让这个嵌套,那就完全不同了。

【讨论】:

  • 是的,这些卡片嵌套很深,所以我想要n 级别的解决方案。谢谢你的回答
  • 以上代码根据最初提供的亲子关系分配了正确的唯一ID。您所要做的就是在每次需要生成具有唯一 ID 的新集合时运行它。但是,如果您想嵌套这个平面结构,请查看我的这个答案 stackoverflow.com/a/37208104/4543207
【解决方案2】:

我想出了一个解决方案,你可以深入研究代码...

代码找到最大 id,然后遍历树,更改 id,如果需要字母数字 id,可以制作某种随机 id 并使用相同的遍历函数。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 2020-03-20
    • 1970-01-01
    • 2020-10-07
    相关资源
    最近更新 更多