简体   繁体   English

Object.assign 无法正确复制

[英]Object.assign does not copy correctly

I'm working with VueJS.我正在使用 VueJS。

I have a Method that receives a Object as argument.我有一个接收 Object 作为参数的方法。

Then I clone this Object with Object.assign() .然后我用Object.assign()克隆这个 Object 。

Component.vue

export default {
  // ...
  methods: {
    // ...
    activateEditMode (item) {
      this.editItemIndex = this.travelItinerary.indexOf(item)
      this.editItem = Object.assign({}, item)
      // ...
    }
  }
}

The original Object at this.roteiroCompleto[0] :原始 Object 在this.roteiroCompleto[0]

在此处输入图像描述

But when I edit the clone Object this.itemEditado :但是当我编辑克隆this.itemEditado

在此处输入图像描述

the original Object this.roteiroCompleto[0] changes too.原来的 Object this.roteiroCompleto[0]也发生了变化。

在此处输入图像描述

I tried to copy each key and value, copy only the Array with .slice() , .map(a=>a) , and nothing works.我尝试复制每个键和值,仅使用.slice().map(a=>a)复制数组,但没有任何效果。 The two objects keep binding.这两个对象保持绑定。

When I console.log(this.itemEditado) , I get this:当我console.log(this.itemEditado) ,我得到这个:

在此处输入图像描述

The strange thing is, in another Vue Component, I use the same strategy, and it works.奇怪的是,在另一个 Vue 组件中,我使用了相同的策略,并且有效。

Object.assign only does a shallow copy of the keys and values, meaning if one of the values in the object is another object or an array, then it is the same reference as was on the original object. Object.assign仅对键和值进行浅拷贝,这意味着如果对象中的一个值是另一个对象或数组,则它与原始对象上的引用相同。

var x = { a: 10, b: { c: 100 } };
var y = Object.assign({}, x);

y.a = 20;
console.log( x.a, y.a ); // prints 10 20

y.b.c = 200;
console.log( x.b.c, y.b.c ) // prints 200 200

To deep copy an object, you can using something like the cloneDeep function in lodash or take an uglier approach using built in functions with JSON.parse( JSON.stringify( obj ) ) .要深度复制对象,您可以使用 lodash 中的cloneDeep函数,或者使用带有JSON.parse( JSON.stringify( obj ) )的内置函数采取更丑陋的方法。

Note that the second option will only work with primitive types that are supported by JSON.请注意,第二个选项仅适用于 JSON 支持的原始类型。

If the methods you used isn't working well with objects involving data types, try this如果您使用的方法不适用于涉及数据类型的对象,请试试这个

import * as _ from 'lodash';

Deep clone object深层克隆对象

myObjCopy = _.cloneDeep(myObj);

来自MDN的解决方案

 Object.assign(this.editItem, JSON.parse(JSON.stringify(item)))

In 2022, to deep clone objects natively on JavaScript you can use structuredClone在 2022 年,要在 JavaScript 上本地深度克隆对象,您可以使用结构化克隆

The global structuredClone() method creates a deep clone of a given value using the structured clone algorithm.全局结构化克隆()方法使用结构化克隆算法创建给定值的深度克隆。

MDN structuredClone() MDN 结构化克隆()

You don't have to use a library, unless you really need a deep copy (I did not need one).你不必使用库,除非你真的需要一个深拷贝(我不需要一个)。 Just do this:只需这样做:

this.editItem = {...item};

The ... operator will decompose item into its keys and values, and since you're doing that in an object literal (the { } ), it uses those as the keys and values of the new object. ...运算符会将item分解为其键和值,并且由于您在对象文字( { } )中执行此操作,因此它将这些用作新对象的键和值。

Might be helpful to other people who, like me, don't need a deep copy.可能对像我这样不需要深拷贝的其他人有帮助。 Object.assign just straight-up doesn't work, and this does.直接使用Object.assign是行不通的,但确实如此。

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

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