Search code examples
loopsgoogle-apps-scriptcomparison

Modify the list elements in google apps script


I have 2 lists as below:

list1 = [01-0160306, 01-0160302]
list2 = [[01-0160299, null, 3496.0, 1.0, null], [01-0160302, null, 18.0, 1.0, null], [01-0160306, null, 18.0, 1.0, null]]

If there are elements in list1, then the corresponding arrays in list2 need to be retrieved and the last element "null" has to be replaced with "Yes". Results need to be pushed back to list2.

Hence, the result has to be

list2 = [[01-0160299, null, 3496.0, 1.0, null], [01-0160302, null, 18.0, 1.0, Yes], [01-0160306, null, 18.0, 1.0, Yes]]

I tried looping over the list1 array and then loop over list2 to get its first element. But not sure how to retrieve those rows, modify and push it back to list2.

if(list1.length > 0){
 for(i=0;i<list1.length;i++){
   for (j=0;j<list2.length;j++){
     if(list2[j][0] == i){ //if there is a match
      //Not sure how to modify and push it back to the list2
     }
  }
}}

I am learning Google Apps Script, any suggestions/leads would be of great help


Solution

  • simply write the new value 'Yes' in the desired array element

    ...
    if(list2[j][0] == i){ //if there is a match
          list2[j][4] = 'Yes'   //Not sure how to modify and push it back to the list2
         }
    ...
    

    JavaScript Array Methods