即使创建了新数组,添加到数组的对象也会相互覆盖

时间:2017-12-06 09:07:57

标签: javascript arrays angularjs push javascript-objects

我有多个成员和多个班级。我试图迭代每个类,并根据每个类搜索所有成员的方便数据;换句话说,每个班级都会重复相同的成员,但每次都有新的值;现在,在计算值后,我正在记录成员,并且值正确如下:

Members Logged

此处记录成员并且InHosp值正确。 但是当我将它们添加到数组中时,似乎会为每个具有相同值的类重复成员,如您所见:

Array_Logged

var Classes = [];


 angular.forEach(MembersAndClasses.Classes, function (value, key) {
     if (value.isChecked == true) {

        var objClass = new Object();
        objClass.ClassName = value.className;

        var arrMembers = new Array(); 
        //var principal = new Object();

        var principal = {};
        principal = MembersAndClasses.prinicipal;
        FillInOutHosp(principal, objClass.ClassName);                        
        arrMembers[0] = principal;
        //console.log(member);
        var counter = 1;

        angular.forEach(MembersAndClasses.FamilyMembers, function (value, key) {
            //var member = new Object();
            var member = {};
            member = value;
            FillInOutHosp(member, objClass.ClassName);        
            //console.log(member);
            arrMembers[counter] = member;
            counter = counter + 1;
            // console.log(arrMembers);
        });

            objClass.Members = arrMembers;
            Classes.push(objClass);
      }
   $scope.Classes = Classes;
 }); 

为什么会这样?每次(在angular.forEach中)我都在创建一个新数组 var arrMembers = new Array();然后我将它添加到我的objClass中。那么这可能是什么问题呢?

0 个答案:

没有答案