JavaScript的回归引用数组项 [英] javascript return reference to array item
问题描述
我有一个这样的数组:
用户= {[ID:1,名称:'名称1'},{ID:2,名称:'名2'}]
我怎么能得到项目的引用{ID:2,名称:'名2'},这样我就可以改变它的name属性,如:
用户= get_item(用户,ID,2);
user.name =user2的名字改为
的console.log(用户)将具有的结果:
[{ID:1,名称:'名称1'},{ID:2,名称:用户2名改为'}]
我试图用Array.filter()函数,但它返回一个新的数组,而不是原来的数组的引用。我不能用变异原数组。
任何想法?
我试图用
Array.filter()
的功能,但它返回一个新的数组,而不是原来的数组的引用。我不能用变异原数组。
块引用>它返回一个新的数组,但数组的项的仍然是相同的对象的引用。因此,
过滤
就好了这一点。VAR filteredUsers = users.filter(功能(输入){返回entry.id === 2;});
变种项= filteredUsers [0];
item.name =用户2名更新;
的console.log(用户[1]。名称)//用户2名更新该数组包含的引用应用于对象,而不是它们的副本。当我们这样做
项目=用户[1]
,或使用过滤器
来获取包含对象的子集的新数组,我们得到的对象引用的副本,所以变量现指同一个对象,像这样的:+ ------- +
|用户|
------- + +
| 0 | ---------> + --------------- +
| | | ID:1 |
| | |名称:名称1| + --------------- +
| | + --------------- + | filteredUsers |
| | + --------------- +
| 1 | ---------> + -------- +< --------- | 0 |
| | | ID:2 | + --------------- +
| | |名称:NAME2|
| | | | + ------ +
| | + --------------- +< --------- |项目|
------- + + + + ------更改对象更改的对象,而这些变化是显而易见的,无论你用它来看看它的属性其参考对象。
:如果你有中的元的数组现在
变种一个= [1,2,3,4,5];
...然后当然,你不能用上面的,因为它们不是对象的方法。在这种情况下,你会使用
的indexOf
查找索引:VAR指数= a.indexOf(3); //找到第一个条目=== 3
...,然后修改条目
A [指数] = 42;
这也适用于字符串(只要他们是好的正常的字符串基元,您通过创建不是东西
新的String()
,其中有几乎从来没有理由这样做)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?
解决方案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 usefilter
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 | +-------+ | 0 |--------->+---------------+ | | | id: 1 | | | | name: "name1" | +---------------+ | | +---------------+ | filteredUsers | | | +---------------+ | 1 |--------->+---------------+<---------| 0 | | | | id: 2 | +---------------+ | | | name: "name2" | | | | | +------+ | | +---------------+<---------| 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).这篇关于JavaScript的回归引用数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!