简体   繁体   English

如何避免包含相同对象的两个数组的反射?

[英]How to avoid reflection of two array which holds the same objects?

I'm having two arrays of same objects, 我有两个相同对象的数组,

var callHistory = [{"CallId":1,"Note":"abcdefghijklmnop"}, {"CallId":2,"Note":"123343"}, {"CallId":3,"Note":"abc123"}];
var tempHistory = [{"CallId":1,"Note":"abcdefghijklmnop"}, {"CallId":2,"Note":"123343"}, {"CallId":3,"Note":"abc123"}];

for(var i = 0; i < tempHistory .length; i++)
{
    var callNote = tempHistory [i]["Note"];
    if(callNote.length > 6)
    {
        callNote = callNote.slice(0, 5);
        tempHistory [i]["Note"] = callNote;
    }
}

Here i'm changing the Note value of tempHistory array of index 0, but i'm getting the reflected response on callHistory also. 在这里,我正在更改索引为0的tempHistory数组的Note值,但是我也在callHistory上得到了反映的响应。

After the end of for loop. for循环结束后。

callHistory = [{"CallId":1,"Note":"abcdef"}, {"CallId":2,"Note":"123343"}, {"CallId":3,"Note":"abc123"}];
tempHistory = [{"CallId":1,"Note":"abcdef"}, {"CallId":2,"Note":"123343"}, {"CallId":3,"Note":"abc123"}];

How it is getting reflected and is there anyway to stop those kind reflection. 它是如何得到反映的,并且无论如何都可以阻止这种反映。

You need to clone the object How do I correctly clone a JavaScript object? 您需要克隆对象如何正确克隆JavaScript对象? :

 function clone(obj) { if (null == obj || "object" != typeof obj) return obj; var copy = obj.constructor(); for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr]; } return copy; } var callHistory = [{"CallId":1,"Note":"abcdefghijklmnop"}, {"CallId":2,"Note":"123343"}, {"CallId":3,"Note":"abc123"}]; var tempHistory = [{"CallId":1,"Note":"abcdefghijklmnop"}, {"CallId":2,"Note":"123343"}, {"CallId":3,"Note":"abc123"}]; for(var i = 0; i < tempHistory .length; i++) { var callNote = clone(tempHistory [i]["Note"]); if(callNote.length > 6) { callNote = callNote.slice(0, 5); tempHistory [i]["Note"] = callNote; } } console.log(callHistory); console.log(tempHistory); 

暂无
暂无

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

相关问题 如何在对象数组中查找持有最大值的对象? - How to find inside an array of objects the object that holds the highest value? jQuery-如何分离依赖同一对象的两个对象? - jQuery - How to separate two objects which are dependent on same object? 如何合并具有相同属性名称的两个对象 - How to merge two objects which have same property name 如何过滤掉两个对象数组之间存在的数据 - How to filter out data which is present between two array of objects 比较两个不同的对象数组并仅从一个数组中过滤出具有相同 ID 的项目 - compare two different array of objects and filter out the item which is having same ID from one array only 如何在javascript中将两个相同长度的数组转换为一个对象数组? - How to convert two arrays of same length into one array of objects in javascript? 如何合并具有相同id的对象和嵌套在数组中的object中的相应数组对象 - How to merge the objects having same id and corresponding array objects which are nested inside the object in a array 如何将具有相同键的两个对象(关联数组)合并到一个对象中 - How to merge two objects (associative array) with same key into one object 如何在javascript中基于相同的键和值合并两个对象数组? - How to Merge two array of objects based on same key and value in javascript? 如何检查对象数组中两个相等的属性是否同时重复? - How to check if in an array of objects two equal properties are repeated at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM