克隆一个对象的元素并推送到它的自身
clone an object's element and push to it's self
我有一个对象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 联合,但没有成功。
您将无法创建您所描述的对象。您在每种情况下都重复 属性 名称 "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];
我认为您需要使用其他密钥,但这行不通吗?
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": ""
}]
}
对象键必须是唯一的,否则你会覆盖以前保存的数据。参见 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>
我有一个对象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 联合,但没有成功。
您将无法创建您所描述的对象。您在每种情况下都重复 属性 名称 "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];
我认为您需要使用其他密钥,但这行不通吗?
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": ""
}]
}
对象键必须是唯一的,否则你会覆盖以前保存的数据。参见 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>