Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help on javascript object arrays.

    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.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    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().
    changed how?

    I’d stab a guess–since your code is not formatted with [code] tags and therefore too hard to read–that it’s due to objects being passed by reference.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Quote Originally Posted by neha.gandhi.us View Post
    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.
    I ran your code with these additions

    Code:
     
     function addByPayCodeAndLaborAcc(selectedTotals) { 
       var uniqueRows = [];
    a=selectedTotals;
       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);
    alert(a==selectedTotals) // alerts true
          } 
       } 
        return uniqueRows;
    }
    it alerts true so selectedTotals
    did not change ?

  • #4
    New Coder
    Join Date
    Apr 2011
    Posts
    49
    Thanks
    0
    Thanked 13 Times in 12 Posts
    Object are passed by reference.
    Code:
    a = {value: true};
    b = a;
    b.value = false;
    alert(a.value); // alerts: false
    
    // So we need to pass a copy of them instead
    a = {value: true};
    b = Object.create(a);
    b.value = false;
    alert(a.value); // alerts: true
    
    // So when you change
    uniqueRows.splice(0,0,selectedTotals[index]);
    // to
    uniqueRows.splice(0, 0, Object.create(selectedTotals[index]));
    
    // it should work as expected


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •