Below is the sample code that i am struggling with:

function addByPayCodeAndLaborAcc(selectedTotals) {

var uniqueRows = [];

for(var index in selectedTotals) {

var summary = findSummaryRow(uniqueRows, selectedTotals[index].pcname, selectedTotals[index].laboracc);

if(summary == null) {

// not found, add a new row.

uniqueRows.splice(0,0,selectedTotals[index]);

} else {

//got it. Just add the amount to summary.

summary.amount = getAmountInTimeOrCurrency(summary.amount, selectedTotals[index].amount);

}

}

return uniqueRows;

}

function findSummaryRow(arrWithUniqueRows, paycode, laboracc) {

for(var index in arrWithUniqueRows) {

if(arrWithUniqueRows[index].pcname == paycode && arrWithUniqueRows[index].laboracc == laboracc) {

return arrWithUniqueRows[index];

}

}

return null;

}

function getAmountInTimeOrCurrency(dupAmount, selectedAmount) {

var totalTime;

if(isAmountInTime(dupAmount)) {

var dupAmountSpilt = dupAmount.split(':');

var selectedAmountSplit = selectedAmount.split(':');

var totalHours = parseInt(dupAmountSpilt[0]) + parseInt(selectedAmountSplit[0]);

var totalMinutes = parseInt(dupAmountSpilt[1]) + parseInt(selectedAmountSplit[1]);

//if minutes is greater then 59, then convert it into an hour.

if (totalMinutes > 59) {

totalHours += Math.floor(totalMinutes / 60);

totalMinutes = totalMinutes % 60;

}

totalTime = totalHours + ':' + padWithZeros(totalMinutes)

}

else {

totalTime = dupAmount + selectedAmount;

}

return totalTime;

}

function isAmountInTime(amount) {

if(amount.indexOf(':') > -1){

return true;

}

return false;

}

function padWithZeros(minutes) {

var minutesLength = (parseInt(minutes) + '').length;

if (minutesLength == 2) return minutes;

else if (minutesLength == 1) return '0' + minutes;

else if (minutesLength == 0) return '00';

else return false;

}

In this example, uniqueRows[] and selectedTotals[] are object arrays and holds key:values. The keys are amount, paycode and laboracc and they have respective values. i am just adding the amount with same paycode and labor acc and adding them to uniqueRows.

The problem i am stuck on is that i don't understand why selectedTotals[] array changed its value after this line summary.amount = getAmountInTimeOrCurrency(summary.amount, selectedTotals[index].amount); in method addByPayCodeAndLaborAcc().

I am not changing selectedTotals value in any of the methods as you can see.

Any help is appreciated.