0

I am trying to update an outer variable inside angular forEach. Here is the code.

var resultMap = {};   
angular.forEach(originalMap, function(value, key) { 
   resultMap[key] = value; 
  }, resultMap);
console.log(resultMap);

Do not know why resultMap is empty even after executing forEach block. Is there anything I am missign here?

Update -- For simplicity I have not mentioned the entire code here. Inside the forEach I will write some conditions to fill the resultMap.

2 Answers 2

2

You are trying to clone object so use angular.copy instead angular.forEach

var resultMap  = angular.copy(originalMap);

If you still want to use for-each

angular.forEach(originalMap, function (value, key) {
      this[key] = value; //change resultMap to this
  }, resultMap);
Sign up to request clarification or add additional context in comments.

Comments

0

I dont know how you check but your code only working here fine. check below link

var resultMap = {};   
var originalMap = {
    id: 20,
    email: '[email protected]'
};
angular.forEach(originalMap, function(value, key) { 
   resultMap[key] = value; 
  }, resultMap);
console.log(resultMap);

jsfiddle

Comments

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.