简体   繁体   English

JavaScript:如何在不替换对象的情况下有效地替换对象的所有值

[英]JavaScript: How to efficiently replace all values of an object without replacing the object

I have a number of objects collected in an array. 我有一些数组收集的对象。 The same objects are also attached to certain DOM elements for various reasons. 出于各种原因,相同的对象也附加到某些DOM元素。 From time to time I need to update one of these objects. 我不时需要更新其中一个对象。 The easiest way to do this is to find the object in the array with the same id property as the one I got new values for through AJAX and then replace it. 最简单的方法是在数组中找到具有相同id属性的对象,通过AJAX获取新值,然后替换它。 But this of course creates a new object and the objects attached to DOM elements are no longer the same. 但这当然会创建一个新对象,并且附加到DOM元素的对象不再相同。 Which means that if I would compare them they would not be the same object anymore. 这意味着,如果我要比较它们,它们将不再是同一个物体。

How can I easiest replace the correct object with the values in the new object without replacing the actual object? 如何最简单地用新对象中的值替换正确的对象而不替换实际对象? (So that the reference remains the same) (这样参考保持不变)

Example of what I don't want 我不想要的例子

var values = [{id:1, name:'Bob'}, {id:2, name:'Alice'}, {id:3, name:'Charlie'}];
var bar = values[2];
console.info(bar === values[0]); // True

var newValue = {id:1, name:'Dave'};
// Somehow find the index of the object with id==3
values[2] = newValue;
console.info(bar === values[2]); // False, should still be true

Only way I can think of is looping through the object with a foreach or something, but hoping there is something built-in to javascript or jQuery or something that allows for more efficient or at least cleaner code. 我能想到的唯一方法是使用foreach或其他东西循环遍历对象,但希望javascript或jQuery内置一些内容,或者允许更高效或更简洁的代码。

You can iterate the values of the new one and set them in the old one: 您可以迭代新值的值并将其设置为旧值:

for (i in newValue) {
    oldValue[i] = newValue[i];
}

You could use the handle/body pattern, so the objects in the array only have a single property, which has all the real properties for the object: 您可以使用句柄/主体模式,因此数组中的对象只有一个属性,该属性具有该对象的所有真实属性:

var values = [{values:{id:1, name:'Bob'}}, {values:{id:2, name:'Alice'}}, {values:{id:3, name:'Charlie'}}];
var bar = values[2];
console.info(bar === values[0]); // True

var newValue = {id:1, name:'Dave'};
// Somehow find the index of the object with id==3
values[2].values = newValue; // set the values of the object, not the object
console.info(bar === values[2]); // Still true

I'd advise you to replace the objects attached to DOM elements at the same time that you replace the objects in your array. 我建议你在替换数组中的对象的同时替换附加到DOM元素的对象。 That way, the comparison still holds true. 这样,比较仍然适用。

If that's not possible, then in your example we can see that you only really replace the name property. 如果那是不可能的,那么在您的示例中我们可以看到您只是真正替换了name属性。 So you just have to copy the name property from the Ajax object to the Array object. 因此,您只需将name属性从Ajax对象复制到Array对象即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM