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> ';
} $('#box1').append(minimizedBox());
}
}
});
-
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– axel.michel2012年12月27日 23:09:19 +00:00Commented Dec 27, 2012 at 23:09
-
possible duplicate of Add to Array jQueryscrappedcola– scrappedcola2012年12月27日 23:15:53 +00:00Commented Dec 27, 2012 at 23:15
3 Answers 3
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> ';
} $('#box1').append(minimizedBox());
}
}
1 Comment
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
append method to work on the object array? I wasn't able to do that.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.