0

I am trying to find some questions to answer and found myself in a pinch when trying to answer one.

The question is to duplicate a row whenever a certain value in a column is found. And the duplicated row should be modified so that the certain value found is changed.

I created this script to solve the said question:

const data = [
  [1, 2, 3, true, 5, 6],
  [7, 8, 9, false, 10, 12]
];

var output = [];

function unexpectedOutput() {
  data.forEach(row => {
    // first push
    output.push(row);
    if (row[3]) {
      row[3] = false;
      // second push IF row[3] is true
      output.push(row);
    }
  });

  console.log(output);
}

Output to be expected here is:

  [ [ 1, 2, 3, true, 5, 6 ],
    [ 1, 2, 3, false, 5, 6 ],
    [ 7, 8, 9, false, 10, 12 ] ]

But I get

  [ [ 1, 2, 3, false, 5, 6 ],
    [ 1, 2, 3, false, 5, 6 ],
    [ 7, 8, 9, false, 10, 12 ] ]

I see that the answer is solved here by redefining a variable from scratch (not equate to row) and that user also experience the same issue but didn't get the explanation as to why the issue happens.

Seems like pointers to me but as I recall, there are nothing like that in JavaScript, so what happened there? The above code is tested in Google Apps Script by the way.

NightEye
  • 10,634
  • 2
  • 5
  • 24
  • `if(row[3]) { row[3] = false;` means that you're always going to be assigning false to it if it's not falsey already... – CertainPerformance Apr 19 '22 at 04:42
  • @CertainPerformance, I may have not explained it properly, what happened is that it modified the previously pushed array, see output samples below the post. – NightEye Apr 19 '22 at 04:43
  • 1
    `push()` doesn't make a copy of the array. You're modifying the same array that was pushed. – Barmar Apr 19 '22 at 04:43
  • 2
    `push([...row])` to clone it otherwise you're modifying the one you pushed – Wyck Apr 19 '22 at 04:43
  • @Barmar, ohh.. I didn't know that I was modifying the same array even though I already pushed it. I though that once pushed, it is a separate entity from the current `row` being modified – NightEye Apr 19 '22 at 04:45
  • 1
    And you're right, it's essentially pointers internally. – Barmar Apr 19 '22 at 04:45
  • 1
    @Wyck, Now I see why that solution works. Since you are creating another entity of where an array contains the elements of the `row` variable. I just didn't know that I was modifying the same entity even if I already pushed it. I really thought it was now a different object. Thanks :) – NightEye Apr 19 '22 at 04:46
  • @Barmar or Wyck, feel free to post your comments as answers so I can upvote/accept it. Thanks :) – NightEye Apr 19 '22 at 04:47
  • 1
    The question has already been closed as a duplicate, it can't be answered. – Barmar Apr 19 '22 at 04:48
  • didn't notice it. Thanks. – NightEye Apr 19 '22 at 04:49
  • 1
    I doubt if push([...row]) works when the row contains non-primitive types such as arrays or objects. In that case, any change to that array or object will be reflected across other pushes as well. – Rishabh Agarwal Apr 19 '22 at 04:50
  • I see that as another issue, but for such simple data types, i think it will suffice to my question. Given that the idea is already understood, solving that underlying issue should not be a problem anymore. Thanks for the insight @Rishabh – NightEye Apr 19 '22 at 04:51

0 Answers0