0

I'm trying to populate an empty array

MessageListFactory = {
    messageListCont: new Array()
}

with literal objects passed by another array as argument of this method

MessageListFactory.init = function(roomList){
    for(var i = 0; i < roomList.length; i++){
        var msgL = MessageList.create();
        msgL.init(roomList[i].jID, roomList[i].roomName);
        console.log(msgL);//Object {messages: Array[0], jID: "c1@xxxx", name: "room S1", notReadMsgCounter: 0, create: function…}
        this.messageListCont.push(msgL);//push msgL to MessageListCont
    }
    console.log(this.messageListCont);//[Object] {length: 0, __proto__: Array[0]}
    }

As you see from the console.log(), if I left the messageListCont empty, it stays as is even after the push(). But if I:

  1. Put an element inside of the array before the for loop (e.g. messageListCont : ["foo"])
  2. Push a string in place of the object (e.g. messageListCont.push("foo"))

In these cases, the push works and the array get populated. Anyone can explain me this?

Nemus
  • 1,322
  • 2
  • 23
  • 47
  • try "messageListCont: []" instead of "new Array" – john Smith Dec 19 '13 at 11:20
  • Maybe `roomList.length == 0`? – Bergi Dec 19 '13 at 11:32
  • @johnSmith Already done, no effects – Nemus Dec 19 '13 at 11:37
  • @Bergi the roomList array contein at least one element, I checked it with a log inside the for loop – Nemus Dec 19 '13 at 11:39
  • Is something emptying your array after the log? The console does only show the current state of objects. Try to log `this.messageListCont.length` instead – Bergi Dec 19 '13 at 11:41
  • @Bergi We're getting something good. actually at the end of the loop, length increase by one (which is correct, as roomList is a single element array), and if I try to accede the 0 indexed element, I'll get the object I pushed in before. The question now is: why a log of the this.messageListCont array itself doesn't show me the object? – Nemus Dec 19 '13 at 11:50
  • @Bergi not sure abaout this. Cause the console actually tell me there is an Object inside. Before loop: console.log(this.messageListCont);//[] after: console.log(this.messageListCont);//[Object] the problem is it doesn't show me what object and its members – Nemus Dec 19 '13 at 12:11
  • Hm, usually you should be able to expand the properties by clicking on a `+` sign or so. Could you include a screenshot maybe? – Bergi Dec 19 '13 at 12:20

0 Answers0