Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript return reference to array item

I have a array like this:

users = [{id:1, name:'name1'},{id:2, name:'name2'}] 

How could I get a reference to the item {id:2, name:'name2'}, so I can change it is name property, like:

user = get_item(users, 'id', 2); user.name = "user2 name changed"; 

console.log(users) will have the result:

[{id:1, name:'name1'},{id:2, name:'user2 name changed'}] 

I tried to use Array.filter() function, but it returns a new array instead of a reference to the original array. which I can not use to mutate the original array.

Any idea?

like image 896
fuyi Avatar asked Jun 19 '14 10:06

fuyi


People also ask

Does JavaScript array find return reference?

To return reference to array item when searching an array of objects with JavaScript, we can use the JavaScript array find method. We have the users array with some objects. And we want to get the 1 with id w and name 'name1' . To do this, we call find with a callback that returns id === 1 && name === 'name1' .

Does array find return a copy or reference?

The find() method returns the value of the first element in the provided array that satisfies the provided testing function. Whether it returns a copy of or a reference to the value will follow normal JavaScript behaviour, i.e. it'll be a copy if it's a primitive, or a reference if it's a complex type.

How do you store the reference of an object in an array?

To set the value, use myRef(<the value you want to set>); . Helpfully, you can also assign this to an array element as well: var myArray = [myRef]; Then use myArray[0]() to read and myArray[0](<new value>) to write.

Does filter return reference?

filter() function, but it returns a new array instead of a reference to the original array.


1 Answers

I tried to use Array.filter() function, but it returns a new array instead of a reference to the original array. which I can not use to mutate the original array.

It returns a new array, but the array's entries are still references to the same objects. So filter is just fine for this.

var filteredUsers = users.filter(function(entry) { return entry.id === 2; }); var item = filteredUsers[0]; item.name = "user2 name updated"; console.log(users[1].name) // "user2 name updated" 

The array contains references to the objects, not copies of them. When we do item = users[1], or use filter to get a new array containing a subset of the objects, we get a copy of the object reference, and so the variable now refers to the same object, like this:

                 +−−−−−−−−−−+ users−−−−−−−−−−>| (array)  |                           +−−−−−−−−−−+           +−−−−−−−−−−−−−−−+                 |   0      |−−−−−−−−−−>|    (object)   |                 |          |           +−−−−−−−−−−−−−−−+                 |          |           | id:   1       |                 |          |           | name: "name1" |                 |          |           +−−−−−−−−−−−−−−−+                 |   1      |−−+                 |          |  |                 |          |  |        +−−−−−−−−−−−−−−−+                 |          |  +−−−+−+−>|    (object)   |                 +−−−−−−−−−−+     / /   +−−−−−−−−−−−−−−−+                                  | |   | id:   2       |                 +−−−−−−−−−−+     | |   | name: "name2" | filteredUsers−−>| (array)  |     | |   +−−−−−−−−−−−−−−−+                 +−−−−−−−−−−+     | |                    |   0      |−−−−−+ |                 +−−−−−−−−−−+       |                                       | item−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ 

Changing the object changes the object, and those changes are visible regardless of which reference to the object you use to look at its properties.

Now if you had an array of primitives:

var a = [1, 2, 3, 4, 5]; 

...then of course, you can't use the approach above because they aren't objects. In that case, you'd use indexOf to find the index:

var index = a.indexOf(3); // Find the first entry === 3 

...and then modify the entry

a[index] = 42; 

This also applies to strings (provided they're nice normal string primitives, not things you created via new String(), which there's virtually never a reason to do).

like image 107
T.J. Crowder Avatar answered Oct 03 '22 23:10

T.J. Crowder