0

While I realize that an array, as a non-primitive data type, is handled by references in JavaScript, not by value, any particular element of that array could be a primitive data type, and I assume then that it is not assigned by reference.

I'd like to know how to get a reference to an individual element in an array so that I don't have to keep referring to the array and the index number while changing that element?

i.e.

var myElement=someArray[4]
myElement=5
//now someArray[4]=5

Am I misinterpreting various docs that imply but do not explicitly state that this is not the intended behavior?

1
  • you can use a getter/setter to turn just about anything into a "var" Commented Nov 7, 2013 at 1:01

2 Answers 2

2

You can make a copy of an array element, but you can't create a value that serves as an alias for an array property reference. That's also true for object properties; of course, array element references are object property references.

The closest you could get would be to create an object with a setter that used code to update your array. That would look something like:

 var someArray = [ ... whatever ... ];

 var obj = {
   set element5(value) {
     someArray[5] = value;
   }
 };

Then:

 obj.element5 = 20;

would update someArray[5]. That is clearly not really an improvement over someArray[5] = 20.

edit — Now, note that if your array element is an object, then making a copy of the element means making a copy of the reference to the object. Thus:

var someArray = [ { foo: "hello world" } ];

var ref = someArray[0];

Then:

ref.foo = "Goodbye, cruel world!";

will update the "foo" property of the object referenced by someArray[0].

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

3 Comments

So anytime I want to change an element of an array, I must access that element in full, explicitly, using the index syntax. It seems wasteful, for example, if you want to do someArray[4].prop=4; someArray[4].foo=6 etc, you must index into the array every time, and if you do a lot of these in succession the program must look up the index every time
If the array element is an object, then making a copy of it will save you that trouble. I'll extend the answer.
Since you're trying to set a property on someArray[4] it seems like that is a non-primitive. In that case you can save off a reference to someArray[4] and use that instead. But as far as saving off a reference to a primitive, you can't do it without some form of boxing like the above answers.
1

You can always pass around a closure to update this:

var myUpdater = function(x) {
  someArray[4] = x;
}
myUpdater(5);

If you want read/write capabilities, box it:

var makeBox = function(arr, n) {
  return {
    read: function() { return arr[n]; },
    write: function(x) { arr[n] = x; }
  };
} 

// and then:
var ptr = makeBox(someArray, 4);
ptr.read(); // original
ptr.write(newValue);
someArray[4]; // newValue

Comments

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.