0

I have

var myFirstArray = [{letter: 'a'}, {letter: 'b'}, {letter: 'c'}];

I would like to make a reference to myFirstArray, so I do this:

var mySecondArray = myFirstArray;

Pushing, splicing, modifying elements in myFirstArray will be shown in mySecondArray, but if I want to reset that array to something else:

myFirstArray = [];

or

myFirstArray = someOtherData;

My reference is lost. I understand why this is, but I want mySecondArray to point at whatever myFirstArray is pointing at, at any time. What's the best way to do this?

My workarounds include:

// Just empty the array and push it:
while (myFirstArray.length) myFirstArray.pop(); 
for (var x in data) myFirstArray.push(x);

// Or put the array in an object:
var viewBag = {};
viewBag.myFirstArray = [...];

var mySecondArray = viewBag.myFirstArray;

I dislike both of these options :(

Edit: Consider another scenario:

function doSomethingToMyArray (theArray) {
  // Say I get new data that will be formatted the way I want it to be
  // Shouldn't I be allowed to do this? 

  var myNewData = [{ letter: 'd' }];
  theArray = myNewData;
}

var myFirstArray = [{letter: 'a'}, {letter: 'b'}, {letter: 'c'}];

doSomethingToMyArray (myFirstArray);

// At this point, myFirstArray will still be a, b, c instead of d
3
  • myFirstArray.length = 0; will clear your Array for both, if that's all that you need to do. If you wish to totally replace the contents then: myFirstArray.length = 0; myFirstArray.push.appy(myFirstArray, someOtherData);. These will both affect mySecondArray as well, provided you're made it reference myFirstArray as your question implies. Commented Feb 6, 2015 at 17:28
  • 1
    Javascript doesn't have variable references. It only has references to objects and arrays. Commented Feb 6, 2015 at 17:29
  • Barmar, ok -- so are you saying there is no way to keep a reference Foo to an array / object Bar when Bar points to a new array / object? Commented Feb 6, 2015 at 17:41

2 Answers 2

1

Why not just set mySecondArray whenever you set myFirstArray?

A good tip for setting variables quickly is this syntax:

var mySecondArray, myFirstArray;

mySecondArray = myFirstArray = ["somevalue"];

In JavaScript, when you set myFirstArray to something like [], and then mySecondArray to myFirstArray, it won't reference the first variable, but both variables will reference the same actual array.

Sign up to request clarification or add additional context in comments.

6 Comments

If you're going to set both variables all the time, what's the point of having two variables?
That is true, Greg Pete, why do you need two variables?
Thanks TRGWII, but the reason is that I am using Angular, and a parent scope owns myFirstArray while a child scope owns mySecondArray -- I want to minimize cross controller communication for these arrays if possible (even if it's through a service or through $watch). mySecondArray must be the same as myFirstArray but uses a different name because it is the name of the variable that a template (which mySecondArray's controller controls) is using.
Hmm, this might not be clean enough, how about a tiny function? mySecondArray = { get: function(){ return myFirstArray; } }; then you could use: mySecondArray.get() whenever you need to reference it from mySecondArray's controller. (Might want to change the name of mySecondArray for readability)
TRGWII - That's a good workaround for the first scenario I laid out. However, what about the second scenario where I am trying to set the array to a new dataset within another method?
|
0

What is happen is that when you set the parent variable to a new value, the watchers are lost and the changes aren't applied to the child.

You need to do this extremely obnoxious convention of putting the file in a data map.

So, instead of:

var myFirstArray

Use:

$scope.data = {}; $scope.data.myFirstArray = blah;

I highly recommend giving the data object a smarter name is possible.

Another problem you could be experiencing is the "dot problem", which is explained in this blog post.

1 Comment

Thanks. While I am using Angular, this is more of a regular Javascript issue. See my edit on the question to demonstrate the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.