-2

I have this complex array of objects (I'll show only the nodes necessary to explain the problem):

var arr = [
    {
        json: {
              doc: {
                   id: 1,
                   atualizacao:{
                               dc: false
                   }
              }
        }
    }
]

angular.forEach(arr, function(value, key) {
    if(value.json.doc.id == 1){
       value.json.doc.atualizacao.dc = true;
    }
});

When I do this forEach, it changes the value of the 'dc' node of the array on a specific position. but at the end of the forEach the array arrstill is still unchanged.

I put many console.logs and found out that:

  • if i log the variable: value.json.doc.atualizacao.dc - IT SHOWS 'true'

  • ; if i log the variable: value.json.doc.atualizacao - IT SHOWS 'dc = true'

  • if i log the variable: value.json.doc - IT SHOWS 'atualizacao.dc = true'

  • if i log the variable: value.json - IT SHOWS 'doc.atualizacao.dc = false'

  • if i log the variable value - IT SHOWS 'json.doc.atualizacao.dc = false'

konkked
  • 3,161
  • 14
  • 19
  • Can you give us a [Plunker](http://plnkr.co/) showing the bug? It would help a lot with trying to duplicate the issue. – Steven Hewitt Jul 21 '16 at 23:12
  • This should be working fine, just tested it in the console ( using `arr.forEach` instead of `angular.forEach` ) – konkked Jul 21 '16 at 23:15
  • see [Why is using “for…in” with array iteration a bad idea?](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea) – Ronnie Royston Jul 21 '16 at 23:17
  • I tryed with arr.forEach and for. Still no change. The stranger thing is that this guy's answer below JUST WORKS. Only not on my project. – Luiz Lindner Jul 21 '16 at 23:41

2 Answers2

0

its changing, take a look here: Plunker

in angular.forEach you can pass just the value for the function.

take a look in the above plunker, hope it helps

  • The most incredible thing is that except by the parameters on the angular.forEach function, the code is the same as mine. But it still dont work for me (even removing that second parameter) Here is a "screenshot" of my code (a bit more complex) that DO NOT WORK. [Screenshot](http://imgur.com/a/Fs5Ip) academias is the "arr" – Luiz Lindner Jul 21 '16 at 23:39
0

I'm starting no think that is a problem with the $rootScope. In fact, the object is much more complex, large and "heavy" then the one I posted above. I think that when I change its value to true, at the end of the forEach it is still "applying" my change. Thats why when I log PART of the object it shows true, and when I log the root object, it show false. It will at least make any sense.