1

enter image description hereI have this code on an angular service. I'm just trying to push an object to an array a given number of times. I'm a bit baffled why this isn't working.

    this.createByeArray = function (lengthOfBracket, numberOfPlayers) {

        var numberOfByes = lengthOfBracket - numberOfPlayers;
        var byeArray = [];
        var bye = {
            name: "bye",
            rank: ""
        };
        for (var i = 0; i < numberOfByes; i++) {
            byeArray.push(bye);
        }
      console.log(byeArray);
      return byeArray;
}

When I log the result, I do seem to get an array with three objects, but I am unable to see either key/value pair on any of the objects when I 'drill down' in the console. Thanks!

Also the parameters I'm passing in are just integers, so that portion of the function is working fine.

*Edit - one more thing I noticed in the console. The final byeArray has a "length" of 0, even though it looks like it has 3 objects on it...

**Edit - Image of my Console added...

**Edit - Here is the code where the function is called on my controller:

$scope.bracketSize = [0, 1, 2, 3, 4, 5, 6, 7];
$scope.players = [object, object, object, object, object];
//each Object is {name: "something", rank: "something} 

$scope.byeArray = mainService.createByeArray($scope.bracketSize.length, $scope.players.length);
Luke Schunk
  • 241
  • 2
  • 13
  • Check if `numberOfByes` is correct and positive number > 0 – Tushar Nov 09 '15 at 03:35
  • You could see `expand` button there..Must be missing it! – Rayon Nov 09 '15 at 03:35
  • There is no key in an array, just an index. And the index is not shown in the console. – rnrneverdies Nov 09 '15 at 03:35
  • @Tushar, He said he *get an array with three objects* – Rayon Nov 09 '15 at 03:36
  • Please, repeat all these steps in your console and make a screenshot of it, where you try to `drill down` and where it looks like it has 3 objects... – Yeldar Kurmangaliyev Nov 09 '15 at 03:48
  • Delete 'this is the first bye array' in `console.log`. Just `console.log(byeArray)`. – iplus26 Nov 09 '15 at 03:50
  • can you provide jsfiddle or plunkr? seems you change your `byeArray` after on code – Grundy Nov 09 '15 at 03:50
  • Also here you output without any string before, so possibly you look some another output? – Grundy Nov 09 '15 at 03:55
  • I think you guys are right - I duplicated the code in JS fiddle and it works fine. It must have something to do with when I'm calling the function on my Controller... The reason it says 'this is the first bye array' is because that's what I have in my production code, but I took it out to post. It's not a log from later in the code – Luke Schunk Nov 09 '15 at 03:56
  • 1
    simple steps to reproduce: `function A(){ var t = [{a:1}]; console.log(t); t.pop(); }` or with splice: `function A(){ var t = [{a:1},{a:1},{a:1}]; console.log(t); t.splice(0,t.length); }` when you can `A()` you see same behavior in console. So, i think you your code also something like this – Grundy Nov 09 '15 at 04:01
  • Thanks @Grundy I'll give it a go – Luke Schunk Nov 09 '15 at 04:16

0 Answers0