-1

I am having trouble with this code and can not figure it out. It appears to add to "data" rather than to "ticket" from "data" like it should. Using code in Google Script

function consolidate(){
  var data = [[1,1,1,1,1,1],[1,1,1,1,1,1],[1,2,1,2,1,2],[1,2,1,2,1,2],[1,1,1,1,1,1],[1,2,1,2,1,2],[1,1,1,1,1,1],[1,2,1,2,1,2],[1,2,1,2,1,2]];
  var ticket = [];
  ticket[0] = data[0];
  for(var x=0;x<data.length;x++){
    for(var i=0;i<ticket.length;i++){
      if(ticket[i][0]==data[x][0]&&ticket[i][1]==data[x][1]&&ticket[i][2]==data[x][2]&&ticket[i][4]==data[x][4]){
        ticket[i][3]=ticket[i][3]+data[x][3];
        ticket[i][5]=ticket[i][5]+data[x][5];
        break;
      } 
    }
    ticket[i]=data[x];
  }
}

my result for data is [[1, 1, 1, 3, 1, 3], [1, 1, 1, 2, 1, 2], [1, 2, 1, 4, 1, 4], [1, 2, 1, 4, 1, 4], [1, 1, 1, 2, 1, 2], [1, 2, 1, 4, 1, 4], [1, 1, 1, 1, 1, 1], [1, 2, 1, 4, 1, 4], [1, 2, 1, 2, 1, 2]] but data should not change

3
  • What is the purpose of ticket[i]=data[x]; if it is outside of the i loop? Commented Sep 30, 2014 at 3:57
  • 1
    Data will change because by doing ticket[0] = data[0]; you are essentially assigning an object reference. Commented Sep 30, 2014 at 3:57
  • OK that makes sense. I was trying to set the first row of data into ticket to start the loop. Thank you! Commented Sep 30, 2014 at 14:22

2 Answers 2

1
function consolidate(){
  var data = [[1,1,1,1,1,1],[1,1,1,1,1,1],[1,2,1,2,1,2],
              [1,2,1,2,1,2],[1,1,1,1,1,1],[1,2,1,2,1,2],
              [1,1,1,1,1,1],[1,2,1,2,1,2],[1,2,1,2,1,2]];
  var ticket = [];
  ticket[0] = data[0].slice();
  for(var x = 0; x < data.length; x++) {
    for(var i = 0; i < ticket.length; i++) {
        for(var k = 0; k < 5; k++) {
         if (ticket[i][k] != data[x][k])
             break;
         else if (k != 4)
             continue;
         ticket[i][3] += data[x][3];
         ticket[i][5] += data[x][5];
      } 
    }
    ticket[i] = data[x].slice();
  }  
  console.log(data);
}

consolidate();
Sign up to request clarification or add additional context in comments.

1 Comment

So this wasn't what we are trying to accomplish. We are searching through rows of 'data'. When we find like data at "columns" 0,1,2, and 4. Then we will add "columns" 3 and 5 from 'data' onto the same columns of 'ticket'. If the loop can not find like "columns" of 0,1,2 and 4 then a new row will be added to 'ticket' resembling that row from 'data'. So we should end up with fewer rows and some rows where "columns" 0,1,2 and 4 are the same will have added totals in "columns" 3 and 5
0

This is where I got and it works. If there is a way to make it better please let me know

function consolidate1(){// put data in here <-----()
  var data = [[1,1,1,1,1,1],[2,1,1,1,1,1],[1,1,1,1,1,1],[2,1,1,1,1,1],[1,1,1,1,1,1],[3,1,1,1,1,1],[1,1,1,1,1,1],[3,1,1,1,1,1],[1,1,1,1,1,1]];
  var ticket = [];
  ticket[0]=[];
  ticket[0][0]=data[0][0];
  ticket[0][1]=data[0][1];
  ticket[0][2]=data[0][2];
  ticket[0][3]=data[0][3];
  ticket[0][4]=data[0][4];
  ticket[0][5]=data[0][5];
  for(var x=1;x<data.length;x++){ //get all the unique rows based on columns 0,1,2,4 
  var indicator = 0;
    for(var i=0;i<ticket.length;i++){
      if(ticket[i][0]==data[x][0] && ticket[i][1]==data[x][1] && ticket[i][2]==data[x][2] && ticket[i][4]==data[x][4]){
        ticket[i][3]=ticket[i][3]+data[x][3];
        ticket[i][5]=ticket[i][5]+data[x][5];
        indicator = 1;
        break;
      }
    }
    if(indicator==0){
      ticket[i]=[];
      ticket[i][0]=data[x][0];
      ticket[i][1]=data[x][1];
      ticket[i][2]=data[x][2];
      ticket[i][3]=data[x][3];
      ticket[i][4]=data[x][4];
      ticket[i][5]=data[x][5];
    }
  }
  Logger.log(ticket);
}

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.