Search code examples
google-apps-scriptsplice

Google Apps Script splice method oddness


splice seems to be creating a dynamic relationship between spliced in elements and the elements they were spliced from ... ???

Say I have an array Array = [ [1 , 2 , 3] ]

I want to duplicate this column and change the last value of the new column to 4. I used: nada = Array.splice(0,0,Array[0]); Array[1][2] = 4;

However, this results in Array = [ [ 1 , 2 , 4] , [ 1 , 2 , 4] ]

This is a stripped down example, in the actual problem I'm iterating through solutions adding possibilities at each step, so nested loops would get messy ...

Insights?? .. or: how do I duplicate a column in the middle of an array and change one of it's values?


Solution

  • I believe your goal is as follows.

    • You want to achieve [[1, 2, 3]] to [[1, 2, 3, 4]] usig Google Apps Script.

    Modification points:

    • First, Array is an object. In this case, I would like to recommend renaming the variable Array.
    • In the case of array.splice(0, 0, array[0]), [1, 2, 3] is inserted to the top element.
    • And, at array[1][2] = 4, 3 of [1, 2, 3] is replaced with 4. Also, in this case, both elements are changed with the pass-by-reference. By this, the result is [ [ 1 , 2 , 4] , [ 1 , 2 , 4] ].

    When these points are reflected in the script, how about the following modifications?

    Modified scripts:

    // If you want to use array[0][3] = 4.
    const array1 = [[1, 2, 3]];
    array1[0][3] = 4;
    console.log(array1); // [ [ 1, 2, 3, 4 ] ]
    
    // If you want to use splice.
    const array2 = [[1, 2, 3]];
    array2.forEach(row => row.splice(row.length, 0, 4));
    console.log(array2); // [ [ 1, 2, 3, 4 ] ]
    
    // Another approach 1
    const array3 = [[1, 2, 3]];
    array3.forEach(row => row.push(4));
    console.log(array3); // [ [ 1, 2, 3, 4 ] ]
    
    // Another approach 3
    const array4 = [[1, 2, 3]];
    const res = array4.map(row => [...row, 4]);
    console.log(res); // [ [ 1, 2, 3, 4 ] ]

    • These scripts return the same result of [ [ 1, 2, 3, 4 ] ] from [[1, 2, 3]].

    Note:

    • I'm not sure whether I could correctly understand your expected result, for example, if you want to achieve [[1, 2, 3]] to [[1, 2, 4]] usig Google Apps Script, how about the following samples?

      // If you want to use array[0][3] = 4.
      const array1 = [[1, 2, 3]];
      array1[0][2] = 4;
      console.log(array1); // [ [ 1, 2, 4 ] ]
    
      // If you want to use splice.
      const array2 = [[1, 2, 3]];
      array2.forEach(row => row[2] = 4);
      console.log(array2); // [ [ 1, 2, 4 ] ]

    References:

    Added:

    From your following reply,

    Actually, I want to achieve [ [ 1,2,3],[1,2,4]], but I think you've pointed me down the path with the "pass by reference" coment. How would I pass by value in array.splice(0, 0, array[0])

    I understood that you wanted to achieve [[1, 2, 3]] to [ [ 1,2,3],[1,2,4]]. In this case, how about the following sample script?

    const array1 = [[1, 2, 3]];
    array1.push(array1[0].slice());
    array1[1][2] = 4;
    console.log(array1); // [ [ 1, 2, 3 ], [ 1, 2, 4 ] ]
    
    const array2 = [[1, 2, 3]];
    array2.push([...array2[0]]);
    array2[1][2] = 4;
    console.log(array2); // [ [ 1, 2, 3 ], [ 1, 2, 4 ] ]
    
    // If you want to use splice.
    const array3 = [[1, 2, 3]];
    array3.splice(array3.length, 0, [...array3[0]]); // array3.splice(array3.length, 0, array3[0].slice());
    array3[1][2] = 4;
    console.log(array3); // [ [ 1, 2, 3 ], [ 1, 2, 4 ] ]
    
    // If you want to use array.splice(0, 0, array[0]) 
    const array4 = [[1, 2, 3]];
    array4.splice(0, 0, [...array4[0]]); // or array4.splice(0, 0, array4[0].slice());
    array4[1][2] = 4;
    console.log(array4); // [ [ 1, 2, 3 ], [ 1, 2, 4 ] ]