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());
        }
    }
});
2
  • 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. Commented Dec 27, 2012 at 23:09
  • possible duplicate of Add to Array jQuery Commented Dec 27, 2012 at 23:15

3 Answers 3

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());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh man, I was thinking my code was wrong but I was declaring my array inside the function. This is why StackOverflow is the best.
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.

2 Comments

Thanks man, this helped allot. I really need to start working with this kind of stuff more often lol.
@ExplosionPills: How did you get append method to work on the object array? I wasn't able to do that.
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.

1 Comment

Duh. Both do the same thing. My mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.