I am just curious about this.
我对此很好奇。
Let's say I have an array of objects and I create 1 object, lets name the array of objects items and the object item.
假设我有一个对象数组,我创建了一个对象,让我们命名对象数组和对象项。
I want to get a particular item in my array of items by using the following code:
我希望使用以下代码在我的项目数组中获取特定项:
//gets an item base on ID
function get_item(td){
var item = undefined;
$.each(items, function(i, val) {
if(val.item_id == td){
item = val;
}
});
return item;
}
The get_item() basically gets an object matched with the supplied id.
get_item()基本上获取与提供的id匹配的对象。
So my question is this. What if I changed the properties of item will it also changed the properties of an object associated with it within the array?
所以我的问题是这个。如果我更改了item的属性,它还会更改数组中与之关联的对象的属性吗?
Thank you very much!
非常感谢你!
3 个解决方案
#1
6
What if I changed the properties of item will it also changed the properties of an object associated with it within the array?
如果我更改了item的属性,它还会更改数组中与之关联的对象的属性吗?
Yes.
Objects are not copied. Instead, references to the objects are passed around. Simplest example:
不复制对象。而是传递对对象的引用。最简单的例子:
var a = [];
var b = a;
b.push(1);
console.log(a); // logs [1]
Many object-oriented programming languages work like this.
许多面向对象的编程语言都是这样工作的。
#2
2
The value of the object inside the array will also change because it's a reference. If you want more information I highly recommend reading Objects and Prototypes.
数组中对象的值也会改变,因为它是一个引用。如果您需要更多信息,我强烈建议您阅读对象和原型。
If you don't want it to change then you should use something like lodash's _.clone()
function.
如果你不想改变那么你应该使用像lodash的_.clone()函数。
Also you could use filter to get the object:
你也可以使用filter来获取对象:
function get_item(td){
return items.filter(function(item) {
return item.id === td;
})[0];
}
#3
1
You can update you function to:
您可以将功能更新为:
var data= array();
function get_item(propertyValue, propertyName){
var retval;
for(var i = 0; i < data.length; i++){
if(data[i][propertyName]==propertyValue){
retval = data[i];
break;
}
}
return retval;
}
Use it
var item1 = get_item(1,"id");
var item2 = get_item("john","name");
#1
6
What if I changed the properties of item will it also changed the properties of an object associated with it within the array?
如果我更改了item的属性,它还会更改数组中与之关联的对象的属性吗?
Yes.
Objects are not copied. Instead, references to the objects are passed around. Simplest example:
不复制对象。而是传递对对象的引用。最简单的例子:
var a = [];
var b = a;
b.push(1);
console.log(a); // logs [1]
Many object-oriented programming languages work like this.
许多面向对象的编程语言都是这样工作的。
#2
2
The value of the object inside the array will also change because it's a reference. If you want more information I highly recommend reading Objects and Prototypes.
数组中对象的值也会改变,因为它是一个引用。如果您需要更多信息,我强烈建议您阅读对象和原型。
If you don't want it to change then you should use something like lodash's _.clone()
function.
如果你不想改变那么你应该使用像lodash的_.clone()函数。
Also you could use filter to get the object:
你也可以使用filter来获取对象:
function get_item(td){
return items.filter(function(item) {
return item.id === td;
})[0];
}
#3
1
You can update you function to:
您可以将功能更新为:
var data= array();
function get_item(propertyValue, propertyName){
var retval;
for(var i = 0; i < data.length; i++){
if(data[i][propertyName]==propertyValue){
retval = data[i];
break;
}
}
return retval;
}
Use it
var item1 = get_item(1,"id");
var item2 = get_item("john","name");