深度复制可观察到的阵列基因敲除 [英] Deepcopying observable array knockout

查看:106
本文介绍了深度复制可观察到的阵列基因敲除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到UI的Observable,单击按钮时,我正在从Observable数组创建一个新数组,并对新数组进行操作,但是不幸的是,新数组中的任何更改也会影响旧数组.

I have a observable which is binded to UI, On button click i am creating a new array from observable array and doing manipulation with new array but unfortunately any changes in new array affects old array too.

我正在使用以下代码进行深度复制.

I am using below code for deep copying.

   var clonedArr = $.extend(true, [], masterArray());

我错过了什么吗?您可以在此处找到jsfiddle( https://jsfiddle.net/t5a1xfud/)了解更多详情

Am i missing something. You can find jsfiddle here (https://jsfiddle.net/t5a1xfud/) for more details

推荐答案

如何进行深度复制(或确保未链接数组): https://jsfiddle.net/t5a1xfud/18/

How to deep copy (or otherwise make sure the arrays aren't linked): https://jsfiddle.net/t5a1xfud/18/

self.newItems(ko.toJS(self.existingItems()));

如何不进行深层复制(还有其他方法): https://jsfiddle.net/t5a1xfud/19/

How to not deep copy (there are other ways too): https://jsfiddle.net/t5a1xfud/19/

self.newItems(self.existingItems());

请注意,在第二个示例中,使用复制到新"按钮后,将链接阵列.它们似乎没有链接,但是如果您在添加到现有"和添加到新的"之间交替单击,将会看到它们确实会相互影响.第一个示例并非如此.

Note that in the second example after you use the "Copy to new" button, the arrays will be linked. They appear not to be linked, but if you alternate clicking between "Add to existing" and "Add to new" you will see that they do affect eachother. Not the case with the first example.

这篇关于深度复制可观察到的阵列基因敲除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆