-1

I've been banging my head on this the whole day, i give up:

I've created a function that receives (amongst others) a source-data-array and a target-data-array, both corresponding to respective sheet-ranges.

I want to extract addresses from the source-array, mold them a bit and unshift the result to the target array, after which the top element is popped. I first tried the recursive approach, but fell back to the while loop because of this. The problem is that with each loop, the addresses are shifted in some weird multiplied way. I dont know how to explain it but check out the log.

    function arrZoekAdressen(strZoekGebied, arrBron, intRij, arrGebiedAdressen, arrActieveStraat) {
  if (arrActieveStraat == undefined) {
    arrActieveStraat = ["Geen adressen", "-"];
  }

  //#1
  while (intRij < arrBron.length) {
    if (arrBron[intRij][0] == strZoekGebied){
      if (arrBron[intRij][2] == arrActieveStraat[0]){
        arrActieveStraat[1] += ", " + arrBron[intRij][3] + arrBron[intRij][4];
      } else {
Logger.log(arrGebiedAdressen);
Logger.log(arrActieveStraat);
        arrGebiedAdressen.unshift(arrActieveStraat);
        arrGebiedAdressen.pop();
Logger.log(arrGebiedAdressen);
        arrActieveStraat[0] = arrBron[intRij][2];
        arrActieveStraat[1] = arrBron[intRij][3] + arrBron[intRij][4];
      }
    }
    intRij++;
  }

  //#2
  arrGebiedAdressen.unshift(arrActieveStraat);
  arrGebiedAdressen.pop();
  return arrGebiedAdressen;
};

The produced log with these loggers looks as follows, check out the array contents, utterly weird!

FIRST LOOP [17-02-07 07:54:11:181 PST] [[, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ]]

[17-02-07 07:54:11:181 PST] [Geen adressen, -]

[17-02-07 07:54:11:182 PST] [[Geen adressen, -], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ]]

SECOND LOOP [17-02-07 07:54:11:183 PST] [[Cuyleborg, 212, 226], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ]]

[17-02-07 07:54:11:183 PST] [Cuyleborg, 212, 226]

[17-02-07 07:54:11:184 PST] [[Cuyleborg, 212, 226], [Cuyleborg, 212, 226], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ]]

THIRD LOOP [17-02-07 07:54:11:185 PST] [[Moselborg, 10, 88], [Moselborg, 10, 88], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ]]

[17-02-07 07:54:11:185 PST] [Moselborg, 10, 88]

[17-02-07 07:54:11:186 PST] [[Moselborg, 10, 88], [Moselborg, 10, 88], [Moselborg, 10, 88], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ], [, ]]

etc. etc.

The source list looks like this:

NR CITY Cuyleborg 212

NR CITY Cuyleborg 226

NR CITY Moselborg 10

NR CITY Moselborg 88

Of course every street should occur only once in the array since you would expect the other streets with every unshift to move up one index.

Please help...

  • array.pop() Removes the last element in an array not the first. To remove the first element use array.shift() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift – Jack Brown Feb 07 '17 at 20:21
  • No that's not what i meant. But i've found the solution: I've been thinking wrong: in javascript when arrays are assigned to variables, they are passed by reference. I used the advise in this post (slice) and now it works like a charm. http://stackoverflow.com/questions/7486085/copying-array-by-value-in-javascript – RiAsRa vdB Feb 07 '17 at 22:14

1 Answers1

0

Solved it myself: javascript passes objects (incl. arrays) to variables by reference. I applied the advice at Copying array by value in JavaScript and now it works like a charm. I should have thought of this earlier, aaaaaaarrrghhh!

Community
  • 1
  • 1