0

I have this piece of code in Javascript / Angular :

 $scope.validCredentials = {            
        'a@a.it': 'aaa',
        'b@b.com' : 'bbb'
    };

I access the variable in this loop:

for ( var k in Object.keys($scope.validCredentials) ) {
                $log.info("K: " +  k);
}

I have read in another question here on StackOverflow that Object.keys would return me the keys of a map, and this is happening somehow, because Firebug during debug correctly gives me the two email addresses.

However the "$log" function gives me 0 (or 1, depending on the cycle), and 0 is also the value that is used later in code, when I check this value against another variable.

Any idea why? What's happening?

I have tested it on firefox and chrome, firebug or developer tools give me the emails list during debug, but the logger logs numbers.

musikele
  • 355
  • 2
  • 15

3 Answers3

1

This is not related to angular, when you use for you are already iterating on the keys so just remove the Object.keys call

var validCredentials = {            
        'a@a.it': 'aaa',
        'b@b.com' : 'bbb'
    };


for ( var k in validCredentials)  {
                console.debug("K: " +  k);
}

this outputs:

K: a@a.it 
K: b@b.com 
Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50
  • I think this is what I wanted to know -- let me get back home and test the solution. – musikele Nov 10 '14 at 09:44
  • It works. However, whith my surprise, If I write `for (var i in anArray) {...}`, `i` is not the content of the array but the index... – musikele Nov 10 '14 at 12:46
  • That is because you are iterating on the properties of the array and not the items it contains. Kind of what you would do with reflection in other languages – Sebastian Piu Nov 10 '14 at 13:27
0

You don't need to iterate. And you're missing a '

var validCredentials = { 'a@a.it': 'aaa', 'b@b.com' : 'bbb'};

console.log( Object.keys(validCredentials));
Dylan
  • 4,703
  • 1
  • 20
  • 23
0

The loop fails because Object.keys() returns an array. See this MDN doc.

You can iterate the array using a standard For Loop

var keys = Object.keys($scope.validCredentials);
var k;

$log.debug(keys);

for (var i = 0; i < keys.length; i++) {
  k = keys[i];

  $log.info("K: " +  k);
  //K: a@a.it
  //K: b@b.com
}
Andrew Johnston
  • 1,109
  • 6
  • 11
  • this works, but I was looking for a way to use the `for...in..` construct in javascript. – musikele Nov 11 '14 at 11:09
  • Yes, the problem with the original code is that you cannot use for ... in ... construct with an array. And Object.keys() returns an array. – Andrew Johnston Nov 11 '14 at 13:47
  • actually you can use it, but one should know that for...in... with arrays iterates over indexes, not on elements ... strange but true (i have to do some other checks before being totally sure of this claim) – musikele Nov 11 '14 at 16:00
  • 1
    You are correct. You can use it, but it's probably not a good idea. The result you get depends on the browser and the way the array in initialized. There is a good answer explaining this [here](http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea) – Andrew Johnston Nov 11 '14 at 17:44