2

I have a JSON array like below

[{
    "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
    }]
}]

now I am doing grouping for this array Shift Wise in below function

I have assign above JSON in

var ParsedJson = above JSON.

I am using below function for this:

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;
};

The first time it gives me a proper result but then also the new data is updated to my original JSON. how to prevent it from being updated?

2 Answers 2

2

You can use jQuery.extend():

var ParsedJson = jQuery.extend(true, {}, YOUR_JSON);
Sign up to request clarification or add additional context in comments.

2 Comments

hi Samurai , i have already used it but facing same problem. the main problem is its updates the data of PaymentDetails which is another array in every shift you can see.
finally this works for me when i use your method in my PaymentDetails array like newItemObj.GroupedPaymentDetails = jQuery.extend(true, {}, item["PaymentDetails"]); //item["PaymentDetails"];
0

Use this :

var secondJSON = Object.create( firstJSON ).

The second json is now independent from the first one.

developer.mozilla.org/object/create

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.