0

This Javascript function inside my class doesn't seem to modify the array that it's passed to it by reference:

this.filterEqualCities(this.birthCitiesNames, this.birthCitiesPositions);

  filterEqualCities: function(citiesNames, citiesPos) {
    var tempNamesArray = [];
    var tempPosArray = [];
    for(var i=0; i<citiesNames.length; i++) {
      var name = citiesNames[i];
      name = name.split(',')[0];
      if(tempNamesArray.indexOf(name) == -1) {
        tempNamesArray.push(name);
        tempPosArray.push(citiesPos[i]);
      }
    }
    citiesNames = [];
    citiesPos = [];
    for(var i=0; i<tempNamesArray.length; i++) {
      citiesNames.push(tempNamesArray[i]);
      citiesPos.push(tempPosArray[i]);
    }
  }
FedFod
  • 3
  • 1
  • Which array is not getting modified? citiesPos? – Nitish Narang Nov 07 '18 at 21:03
  • 2
    Well it seems you are reassigning array to empty; citiesNames = []; citiesPos = []; and that's why its losing reference – Nitish Narang Nov 07 '18 at 21:04
  • you can simply do citiesNames.length = 0 to retain reference and empty array parallely – Nitish Narang Nov 07 '18 at 21:05
  • 1
    `citiesNames` is the array as it's passed in, but towards the end of the function you have `citiesNames = []`, which means it is now a different array. – user3094755 Nov 07 '18 at 21:05
  • Thanks for the answer! It doesn't work even if I do that: var citiesLenDiff = citiesNames.length - tempNamesArray.length; citiesNames.splice(-1, citiesLenDiff); for(var i=0; i – FedFod Nov 07 '18 at 21:11
  • It is not that if I pass an object it should get modified inside the function? – FedFod Nov 07 '18 at 21:12
  • Possible duplicate of [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Herohtar Nov 07 '18 at 21:12
  • I think you are conflating pass-by-reference/value, reference type values and mutable values. These are all different things. JavaScript objects are mutable, reference-type values. That means that multiple "places" can have a reference to the same object and each "place" can mutate it. JavaScript is still a pass-by-value language. *Pass-by-x* really only describes the relationship between variables/parameters, which is independent of their values. – Felix Kling Nov 07 '18 at 21:13

2 Answers2

1

When you do:

citiesNames = [];
citiesPos = [];

the variables are no longer references to the original arrays that were passed in, now they're references to these two empty arrays.

If you want to clear out the original arrays, you can simply set their lengths:

citiesNames.length = 0;
citiesPos.length = 0;
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Sorry I'm new to stackoverflow, how can I set your answer as the accepted one? – FedFod Nov 08 '18 at 16:55
  • Since I'm a new member with less than 15 reputation points, my vote will be recorded but not publicly displayed – FedFod Nov 09 '18 at 09:33
0

To expand a bit upon @Barmar's excellent answer - the issue is that arrays and other objects are not really "passed by reference" in Javascript, even though it seems that way a lot of the time, and it's often described that way. They're passed by value, like all other variables - but that "value" is itself a "reference" to an actual value stored in memory, not to another variable itself.

So if you were to modify citiesNames, say by pushing a new element onto it, that would be reflected outside the function - because you'd be modifying that shared value to which both citiesNames variables (local and global) are references to.

But when you do citiesNames=[], you're taking the citiesNames local variable and reassigning it to a completely new value, and there's no way your other citiesNames variable can know about that.

This behaviour isn't unique to Javascript. Python certainly behaves the same way (and perhaps others too that I'm not as familiar with).

Robin Zigmond
  • 17,805
  • 2
  • 23
  • 34