【问题标题】:How to copy all its parent objects from the array of objects using the lodash or javascript如何使用 lodash 或 javascript 从对象数组中复制其所有父对象
【发布时间】:2017-06-19 02:54:57
【问题描述】:

您好,我有一个像这样的对象,我想复制它的所有父对象,

permisions= [
 {
        "parent_id": "",
        "id": "DISTRIBUTOR1",
        "city":01
        "permission": "NO",
    },
    {
        "parent_id": "",
        "id": "DISTRIBUTOR1",
        "city":02
        "permission": "NO"
    },
    {
        "parent_id": "",
        "id": "DISTRIBUTOR1",
        "city":03 
        "permission": "Yes"
    },
    {
        "parent_id": "",
        "id": "DISTRIBUTOR1",
        "city":04
        "permission": "Yes"

    },
    {
        "parent_id": "DISTRIBUTOR1",
        "id": "DISTRIBUTOR2",
        "city":0111            
        "permission": "NO"
    },
    {
        "parent_id": "DISTRIBUTOR1",
        "id": "DISTRIBUTOR2",
        "city":0112                
        "permission": "Yes"
    },


    {
        "parent_id": "DISTRIBUTOR2",
        "id": "DISTRIBUTOR3",
        "city":0333                  
        "permission": "Yes"
    },
    {
        "parent_id": "DISTRIBUTOR2",
        "id": "DISTRIBUTOR3",
        "city":01111                 
        "permission": "Yes"
    },
    {
        "parent_id": "",
        "id": "DISTRIBUTOR4",
        "city":0444                
        "permission": "Yes"
    },
    {
        "parent_id": "",
        "id": "DISTRIBUTOR5",
        "city":0555              
        "permission": "Yes"
    }
]

如果我选择 id:Distubutor3 它包含 parent_id:"DISTRIBUTOR2" 并且 DISTRIBUTOR2 包含父_id: DISTRIBUTOR1 所以它必须复制所有父对象直到结束,任何人都可以帮助我。输出应该是这样的,

result=[   {
        "parent_id": "",
        "id": "DISTRIBUTOR1",
        "city":01
        "permission": "NO",
    },
    {
        "parent_id": "",
        "id": "DISTRIBUTOR1",
        "city":02
        "permission": "NO"
    },
    {
        "parent_id": "",
        "id": "DISTRIBUTOR1",
        "city":03 
        "permission": "Yes"
    },
    {
        "parent_id": "",
        "id": "DISTRIBUTOR1",
        "city":04
        "permission": "Yes"

    },
    {
        "parent_id": "DISTRIBUTOR1",
        "id": "DISTRIBUTOR2",
        "city":0111            
        "permission": "NO"
    },
    {
        "parent_id": "DISTRIBUTOR1",
        "id": "DISTRIBUTOR2",
        "city":0112                
        "permission": "Yes"
    },

    {
        "parent_id": "DISTRIBUTOR2",
        "id": "DISTRIBUTOR3",
        "city":0333              
        "permission": "Yes"
    },
        {
        "parent_id": "DISTRIBUTOR2",
        "id": "DISTRIBUTOR3",
        "city":01111                 
        "permission": "Yes"
    }]

【问题讨论】:

  • 您是要检查该 ID 是否存在于数组中,还是以任何方式进行分配。我的意思是如果没有DISTRIBUTOR1 是否应该DISTRIBUTOR2 获得该属性?
  • 如果DISTRIBUTOR2不存在,DISTRIBUTOR3是否应该获取DISTRIBUTOR1的属性?
  • @ibrahimmahrir 否,如果 DISTRIBUTOR3 不包含 parent_id,则不应复制任何内容,
  • @ibrahimmahrir DISTRIBUTOR3 包含 parent_id 作为 DISTRIBUTOR2 并且 DISTRIBUTOR2 包含 parent_id 作为 DISTRIBUTOR1,因此它必须复制所有 DISTRIBUTOR2 和 DISTRIBUTOR1 对象。
  • ID 总是采用DISTRIBUTOR??? 的形式,其中??? 是数字。

标签: javascript arrays node.js underscore.js lodash


【解决方案1】:

var permissions = [{"parent_id":"","id":"DISTRIBUTOR1","city":1,"permission":"NO"},{"parent_id":"","id":"DISTRIBUTOR1","city":2,"permission":"NO"},{"parent_id":"","id":"DISTRIBUTOR1","city":3,"permission":"Yes"},{"parent_id":"","id":"DISTRIBUTOR1","city":4,"permission":"Yes"},{"parent_id":"DISTRIBUTOR1","id":"DISTRIBUTOR2","city":73,"permission":"NO"},{"parent_id":"DISTRIBUTOR1","id":"DISTRIBUTOR2","city":74,"permission":"Yes"},{"parent_id":"DISTRIBUTOR2","id":"DISTRIBUTOR3","city":219,"permission":"Yes"},{"parent_id":"DISTRIBUTOR2","id":"DISTRIBUTOR3","city":585,"permission":"Yes"},{"parent_id":"","id":"DISTRIBUTOR4","city":292,"permission":"Yes"},{"parent_id":"","id":"DISTRIBUTOR5","city":365,"permission":"Yes"}];

// get the ID number ("DISTRIBUTOR???" return "???")
function idNum(id) {
  return parseInt(id.substr(11));
}

function makeResult(perm, id){
  var num = idNum(id);
  return perm.map(function(p){
    var n = idNum(p.id);
    if(n < num && n != 1) // if the current object's ID is 1 < ID < max
      p.parent_id = p.parent_id || ("DISTRIBUTOR" + (n-1)); // fill id if it's not already set
    return p;
  });
}

var res = makeResult(permissions, "DISTRIBUTOR5"); // set all objects that have ID (1 < ID < 5)

console.log(res)

注意: 例如,如果您调用 makeResult(permissions, "DISTRIBUTOR5"); 并且数组中没有具有 "DISTRIBUTOR2" 的对象,则对象 "DISTRIBUTOR3" 将其 parent_id 设置为 @987654326 @无论如何。

【讨论】:

  • 不,在 DISTRIBUTOR5 的情况下,它不包含 parent_id,因此它只会返回其具有 id:DISTRIBUTOR5 的对象
【解决方案2】:

编辑:我认为你的意思是只有那些父元素存在的元素,连同元素本身,才应该包含在新的输出数组中。

如果是这样,试试这个:

var filterParents = function(obj){

    var mappings = {};

    permisions.forEach(function(perm){

        if( !mappings[perm.id] )
            mappings[perm.id] = [];

        mappings[perm.id].push( perm );
    });

    var output = [];
    output.push( obj );


    while(obj.parent_id && obj.parent_id != '' )
    {
        output = output.concat( mappings[ obj.parent_id ] );
        obj = mappings[ obj.parent_id ] [ 0 ];
    }

    return output;

};

这是假设父母 id 的每个元素都具有相同的祖父母 id。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多