0

When I set imArray and console.log() it out, the console tells me that each value from members.[i1].friendId is at value 0. I have about 5 values that pass to imArray. This is my first time working with dynamic array looping. So if you could, please explain to me how I'd output this data into an array correctly.

$.getJSON("<?=base_url()?>index.php/regUserDash/chatBoxMembers", function(data) {
    var members = data.chatMembers;
    for(var i1 = 0; i1 < members.length; i1++) {
        var imArray = [members[i1].friendId];
        if(members[i1].activityStatus === 'Minimized') {
            function minimizedBox() {
                return '<span style="position: relative; left: 84px; font-family: Verdana; font-size: 8pt" class="clickForMessageBox">' + members[i1].firstname + ' ' + members[i1].lastname + '</span>&nbsp;&nbsp;&nbsp;';
            } $('#box1').append(minimizedBox());
        }
    }
});
Michael Grigsby
  • 11,467
  • 9
  • 33
  • 52
  • You want to add the members[i1].friendId to an array imArray? In this case you should use imArray.push(members[i1].friendId) and you have to be sure that you do not declare the array again for each loop you do. – axel.michel Dec 27 '12 at 23:09
  • possible duplicate of [Add to Array jQuery](http://stackoverflow.com/questions/5861859/add-to-array-jquery) – scrappedcola Dec 27 '12 at 23:15

3 Answers3

2

Declare the array outside and then you can push the data in during each cycle:

var imArray = []; 

for(var i1 = 0; i1 < members.length; i1++) {
    //var imArray = [members[i1].friendId];
    imArray.push(members[i1].friendId);
    if(members[i1].activityStatus === 'Minimized') {
        function minimizedBox() {
            return '<span style="position: relative; left: 84px; font-family: Verdana; font-size: 8pt" class="clickForMessageBox">' + members[i1].firstname + ' ' + members[i1].lastname + '</span>&nbsp;&nbsp;&nbsp;';
        } $('#box1').append(minimizedBox());
    }
}
Nope
  • 22,147
  • 7
  • 47
  • 72
1

I think that you want to append the values to the array:

var imArray = [];
for (var i1 = 0 ...
   imArray.append(members[i1].friendId]);

With your code you are just setting imArray to an array with a single value each time.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
0

Your code seems to be putting the returned data into a visible array on screen. If that's what youare doing you might want to use this link for more info on how to add to the DOM to make some stuff visible on screen.

This part:

$('#box1').append(minimizedBox());

turns into something like:

$(minimizedBox()).appendTo('#box1');

Which takes the generated HTML and puts it in the box.

Lee Meador
  • 12,829
  • 2
  • 36
  • 42