gpt4 book ai didi

javascript - 如何在不更改现有 json 数组值的情况下克隆 json 数组

转载 作者:行者123 更新时间:2023-11-29 21:49:03 25 4
gpt4 key购买 nike

我有一个像下面这样的 JSON 数组

[{
"shiftNo": "shift 1",
"UserId": 4,
"UserName": "Felicia Frazier",
"opnAmt": 0,
"ClsAmt": 0,
"RegisterName": "kano",
"RegisterId": 2,
"ZId": 19,
"OpenDate": "2014-11-24 08:51:56.000",
"CloseDate": "2014-11-24 16:58:02.000",
"Total": 21.83,
"PaymentDetails": [{
"PayId": 1,
"PaymentName": "Cash",
"PaidAmount": 0
}, {
"PayId": 2,
"PaymentName": "Check",
"PaidAmount": 0
}, {
"PayId": 3,
"PaymentName": "Credit Card",
"PaidAmount": 21.83
}]
},

{
"shiftNo": "shift 1",
"UserId": 5,
"UserName": "Dewayne Frazier",
"opnAmt": 0,
"ClsAmt": 0,
"RegisterName": "kano",
"RegisterId": 2,
"ZId": 19,
"OpenDate": "2014-11-24 08:51:56.000",
"CloseDate": "2014-11-24 16:58:02.000",
"Total": 26.2,
"PaymentDetails": [{
"PayId": 1,
"PaymentName": "Cash",
"PaidAmount": 5.45
}, {
"PayId": 2,
"PaymentName": "Check",
"PaidAmount": 0
}, {
"PayId": 3,
"PaymentName": "Credit Card",
"PaidAmount": 20.75
}]
},


{
"shiftNo": "shift 2",
"UserId": 5,
"UserName": "Dewayne Frazier",
"opnAmt": 202.91,
"ClsAmt": 0,
"RegisterName": "kano",
"RegisterId": 6,
"ZId": 20,
"OpenDate": "2014-11-24 10:32:23.000",
"CloseDate": "2014-11-24 22:01:37.000",
"Total": 2380.69,
"PaymentDetails": [{
"PayId": 1,
"PaymentName": "Cash",
"PaidAmount": 818.65
}, {
"PayId": 2,
"PaymentName": "Check",
"PaidAmount": 90.1
}, {
"PayId": 3,
"PaymentName": "Credit Card",
"PaidAmount": 1471.94
}]
},

{
"shiftNo": "shift 2",
"UserId": 7,
"UserName": "Kelvin Daniels",
"opnAmt": 100,
"ClsAmt": 0,
"RegisterName": "kano",
"RegisterId": 6,
"ZId": 20,
"OpenDate": "2014-11-24 10:32:23.000",
"CloseDate": "2014-11-24 22:01:37.000",
"Total": 1516.33,
"PaymentDetails": [{
"PayId": 1,
"PaymentName": "Cash",
"PaidAmount": 1516.33
}, {
"PayId": 2,
"PaymentName": "Check",
"PaidAmount": 0
}, {
"PayId": 3,
"PaymentName": "Credit Card",
"PaidAmount": 0
}]
}]

现在我在下面的函数中对这个数组 Shift Wise 进行分组

我已经在

中分配了上面的 JSON

var ParsedJson = above JSON.

我为此使用以下函数:

var SubGridResult = getShiftReportSubGridGroupedValue(ParsedJson, groupColumnName, RegisterName);

function getShiftReportSubGridGroupedValue(sourceResult, groupColumnName, RegisterName) {

//ParsedJson.filter(function (v) { return v[groupColumnName] == getRemovedCountFormatedValue(jQuery('#tblShiftSummaryReport').jqGrid('getCell', row_id, groupColumnName).trim()) }),
var pGroupedResult = new Array();
var GroupedResult1 = sourceResult.slice();
var GroupedResult1234 = GroupedResult1.filter(function (v) { return v[groupColumnName] == RegisterName });

$.each(GroupedResult1234, function (index, item) {
var itemResult = new Array();
itemResult = pGroupedResult.filter(function (value) { return value["shiftNo"] == item["shiftNo"] });
if (itemResult.length > 0) {
itemResult[0].ItemCount++;
itemResult[0].opnAmt += parseFloat(item["opnAmt"]);
itemResult[0].ClsAmt += parseFloat(item["ClsAmt"]);
itemResult[0].Total += parseFloat(item["Total"]);
var itemmm = itemResult[0].GroupedPaymentDetails;
$.each(itemResult[0].GroupedPaymentDetails, function (indexIn, itemIn) {
itemResult[0].GroupedPaymentDetails[indexIn]["PaidAmount"] += parseFloat(item["PaymentDetails"][indexIn]["PaidAmount"]);
});
} else {
var newItemObj = new Object();
newItemObj.ItemCount = 1;
newItemObj.shiftNo = item["shiftNo"];
newItemObj.RegisterName = item["RegisterName"];
newItemObj.opnAmt = parseFloat(item["opnAmt"]);
newItemObj.ClsAmt = parseFloat(item["ClsAmt"]);
newItemObj.GroupedPaymentDetails = new Array();
newItemObj.GroupedPaymentDetails = item["PaymentDetails"].slice();
newItemObj.Total = parseFloat(item["Total"]);



pGroupedResult.push(newItemObj);
}
});
return pGroupedResult;
};

第一次它给了我正确的结果,但随后新数据也更新为我的原始 JSON。如何防止它被更新?

最佳答案

您可以使用 jQuery.extend() :

var ParsedJson = jQuery.extend(true, {}, YOUR_JSON);

关于javascript - 如何在不更改现有 json 数组值的情况下克隆 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30093329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com