-2

So I've read many problems on here about people losing their variable's value, and it has something to do with closure. But I am completely unable to identify the problem here. I've tried with for loops, while loops, neither works.

function parseArray(arrayIn) {
    var firstRow = arrayIn.shift();

    while ( arrayIn.length > 0 ) {
        addToTable(firstRow, arrayIn.shift());
    }
}

I've also tried

function parseArray(arrayIn) {
    var firstRow = Object.assign([], arrayIn[0]);

    for ( var i=1; i<arrayIn.length; i++) {
        addToTable(firstRow, arrayIn[i]);
    }
}

Either way, addToTable is called twice correctly, and on the third pass, firstRow is an empty array. I originally tried sending arrayIn[0] which also became an empty array on the third pass. The strangest thing is, the second value (arrayIn.shift() or arrayIn[i]) is the correct and expected value the whole time.

This is probably something simple I'm just missing, but, can anyone help? I've been unable to see how other answers to do with closure apply to this case.

Phil
  • 157,677
  • 23
  • 242
  • 245
Jesse Fogel
  • 75
  • 2
  • 10
  • Java is not Javascript. – user2357112 Oct 26 '17 at 00:02
  • 1
    Why are you destroying the input in `parseArray`? – user2357112 Oct 26 '17 at 00:02
  • And what is `addToTable`? – user2357112 Oct 26 '17 at 00:04
  • This is javscript, not java – Jesse Fogel Oct 26 '17 at 00:04
  • Destroying the input? I believe you're referring to my while loop. It was an attempt to get this to work correctly. I do not need the input outside the loop. However, my other attempt does not destroy the input and simply loops through it. Still does not work – Jesse Fogel Oct 26 '17 at 00:06
  • addToTable is a function. It's working correctly so does not need to be here in my example. – Jesse Fogel Oct 26 '17 at 00:06
  • Variables don't "loose" their values. What is the value of `arrayIn` ? What does `addToTable` do? What input does it expect? – Felix Kling Oct 26 '17 at 00:13
  • arrayIn is an array of arrays. addToTable expects two arrays. After addToTable is called twice, so after the loop as ran twice, it's always called with a blank array in the first parameter. So firstRow always becomes [ ] after the loop runs twice – Jesse Fogel Oct 26 '17 at 00:17
  • so it is probably addToTable() that is making firstRow become [], so if you post that code we can see. Since it is not this code. – Nick Timmer Oct 26 '17 at 00:24
  • 1
    Thank you for your response Nick, you're right, addToTable() is changing firstRow. How do I get around this so that firstRow is always the same on each call to addToTable()? – Jesse Fogel Oct 26 '17 at 00:29

3 Answers3

1

How do I get around this so that firstRow is always the same on each call to addToTable()? – Jesse Fogel

Well if you cannot change the method addToTable() at all. Here is what I would try:

function parseArray(arrayIn) {
    var firstRow = arrayIn.shift();

    while ( arrayIn.length > 0 ) {
        var copy = firstRow.slice();
        addToTable(copy, arrayIn.shift());
    }
}

This is how to copy an array by value( not reference): as noted here: Copying array by value in JavaScript

Nick Timmer
  • 425
  • 2
  • 12
0

You should start your loop from 0 not 1:

function parseArray(arrayIn) {
    var firstRow = Object.assign([], arrayIn[0]);

    for ( var i=0; i<arrayIn.length; i++) {
        addToTable(firstRow, arrayIn[i]);
    }
}
  • This is not relevant to my question. Also, I specifically start my loop at 1 because 0 is assigned to first row, I don't want to call addToTable(arrayIn[0], arrayIn[0]) on the first pass of the loop – Jesse Fogel Oct 26 '17 at 00:08
0

The solution to this is as follows: rather than using Object.assign to set the value of firstRow, use Object.assign to call addToTable() e.g.

function parseArray(arrayIn) {
    firstRow = arrayIn[0];

    for ( var i=1; i<arrayIn.length; i++ ) {
        addToTable(Object.assign([], firstRow), arrayIn[i]);
    }
}
Jesse Fogel
  • 75
  • 2
  • 10
  • 1
    so this worked for you then? I posted another option but this looks like it works too. another way of passing a copy by value of the array so when addToTable() changes the passed in array you still have the original firstRow Array intact. – Nick Timmer Oct 26 '17 at 16:24
  • 1
    Good Job figuring out a solution, can you accept your answer so people know this thread is done. Thank you! – Nick Timmer Oct 26 '17 at 16:36