0

I am using angularjs 1.5 and in that trying to copy one object in another variable. To copy the variable, I am using angular.copy() function. The destination variable is not getting all the values which is there in source.

Below is my code

$scope.searchCond = {
    group_id:[],
    sections:[]      
};
for(var i=1;i<5;i++)  {   
   $scope.searchCond.sections[i+"_sec"]=[];
   $scope.searchCond.sections[i+"_sec"]["section_id"]=[];
   $scope.searchCond.sections[i+"_sec"]["section_id"].push(i);
};
var tmpVar = angular.copy($scope.searchCond);
console.log(tmpVar);
console.log($scope.searchCond);

Output of both the console are given below

OutPut of $scope.searchCond

{group_id: Array(0), sections: Array(0)}
group_id:[]
sections:Array(0)
1_sec:[section_id: Array(1)]
2_sec:[section_id: Array(1)]
3_sec:[section_id: Array(1)]
4_sec:[section_id: Array(1)]

OutPut of tmpVar

{group_id: Array(0), sections: Array(0)}
group_id:[]
sections:Array(0)
length:0

tmpVar is not copying sections(1_sec,2_sec) from the source object $scope.searchCond

Is there any solution for this problem?

1
  • can you make a codepen out of our problem and post it here? your code isn't even filling the first object here codepen.io/OmarEinea/pen/ajVdZp Commented Jul 28, 2018 at 7:25

1 Answer 1

1
$scope.searchCond = {
    group_id:[],
    ̶s̶e̶c̶t̶i̶o̶n̶s̶:̶[̶]̶
    sections:{}      
};
for(var i=1;i<5;i++)  {   
   ̶$̶s̶c̶o̶p̶e̶.̶s̶e̶a̶r̶c̶h̶C̶o̶n̶d̶.̶s̶e̶c̶t̶i̶o̶n̶s̶[̶i̶+̶"̶_̶s̶e̶c̶"̶]̶=̶[̶]̶;̶
   $scope.searchCond.sections[i+"_sec"]={};
   $scope.searchCond.sections[i+"_sec"]["section_id"]=[];
   $scope.searchCond.sections[i+"_sec"]["section_id"].push(i);
};
var tmpVar = angular.copy($scope.searchCond);
console.log(tmpVar);
console.log($scope.searchCond);

The angular.copy function only copies the numeric properties of an array. If you want the property names to be non-numeric, initialize it to be an object.

Sign up to request clarification or add additional context in comments.

4 Comments

I have one more query on this. I was trying to display length of the object in very next line for $scope.searchCond.sections which already have ("1_sec","2_sec" etc) in it but it always returning 0 / undefined what could be the reason?
Array.length is a property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array. It does not count non-numeric property names, only property names that can be parsed as an integer.
Got It Thanks. Then how do I get the length of the objects? I tried with Objects.keys and I am getting it accurately. Is there any-other way through angularjs? I wan to traverse through all the properties in the object.
The angular.copy function is an internal function that the AngularJS team made available for users. It has limited uses. If you want to do more complicated object/array manipulation, consider using another library such as lodash or underscore. Or do it in plain old JavaScript. In any case, the question of how to count the number of properties in an object should be asked as a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.