【问题标题】:clone an object's element and push to it's self克隆一个对象的元素并推送到它自己
【发布时间】:2015-10-13 19:36:18
【问题描述】:

我有一个对象 A。

如果我JSON.stringify(A),我会得到这个结果:

{
    "OrderA": [{
        "orderId": "19",
        "serverId": 129,
        "description": "Apple",
        "status": "1",
        "details": ""
    }]
}

现在,我想克隆它自己的元素并制作一个更大的对象,如下所示:

{
    "OrderA": [{
        "orderId": "19",
        "serverId": 129,
        "description": "Apple",
        "status": "1",
        "details": ""
    }],
    "OrderA": [{
        "orderId": "19",
        "serverId": 129,
        "description": "Apple",
        "status": "1",
        "details": ""
    }],
     "OrderA": [{
        "orderId": "19",
        "serverId": 129,
        "description": "Apple",
        "status": "1",
        "details": ""
    }]
}

如何使用 lodash/underscore/ 或 jQuery 来实现?

到目前为止,我已经尝试过 jQuery.extend、lodash union,但没有奏效。

【问题讨论】:

  • 那不是一个有效的对象;键应该是唯一的,例如OrderA, OrderB, OrderC.....
  • 结构不应该是"OrderA":[{…}, {…}, {…}]而不是"OrderA":[{…}], "OrderA":[{…}], "OrderA":[{…}]吗?
  • @Xufox 哦,我只是好奇如何克隆对象的元素,结果发现我们不能有双键

标签: javascript jquery underscore.js lodash


【解决方案1】:

您将无法创建您所描述的对象。您在每种情况下都重复属性名称"OrderA",这是不允许的。我建议使用一个新数组并将对象推入其中,而不是使用具有属性名称的映射对象。像这样粗糙的东西:

var A; //Your OrderA Object.
var B = jQuery.extend(true, {}, A),
    C = jQuery.extend(true, {}, A),
    D = jQuery.extend(true, {}, A);

var arr = [A, B, C, D];

【讨论】:

    【解决方案2】:

    对象键必须是唯一的,否则您会覆盖以前保存的数据。见DEMO

    方法如下:

    var ALL = {
        "OrderA": [{
            "orderId": "19",
            "serverId": 129,
            "description": "Apple",
            "status": "1",
            "details": ""
        }]
    };
    
    ['B','C','D'].forEach(function(letter,index) {
        ALL['Order'+letter] = $.extend(true, {}, ALL.OrderA);
    });
    
    console.log( JSON.stringify( ALL ) );
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    【讨论】:

    • 这里没有克隆对象 - OrderA、OrderB 等都引用同一个对象。
    【解决方案3】:

    我认为您需要使用其他密钥,但这不起作用?

    var A = {
        "OrderA": [{
            "orderId": "19",
            "serverId": 129,
            "description": "Apple",
            "status": "1",
            "details": ""
        }]
    };
    
    var B = {};
    
    for (var attr in A.OrderA[0]) {
      if (A.OrderA[0].hasOwnProperty(attr)) {
        B[attr] = A.OrderA[0][attr];
      }
    }
    
    A.OrderB = [B];
    
    JSON.stringify(A);
    
    {
        "OrderA": [{
            "orderId": "19",
            "serverId": 129,
            "description": "Apple",
            "status": "1",
            "details": ""
        }],
        "OrderB": [{
            "orderId": "19",
            "serverId": 129,
            "description": "Apple",
            "status": "1",
            "details": ""
        }]
    }
    

    【讨论】:

    • 如果他稍后更改 A.OrderA 属性,那也会更改 A.OrderB 属性,因为它们共享对对象的相同引用。
    • 嗨,我试过了,得到了'Uncaught TypeError: Cannot set property '0' of undefined' 错误。
    • 抱歉,错过了 OrderA 上的数组包装器。检查编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 2015-09-16
    • 2015-03-05
    • 2019-09-21
    • 2013-01-26
    相关资源
    最近更新 更多