1

Okay, so I'm a JSON noob with only basic jQuery knowledge and I've looked all over for a solution and can't find one. Any help is greatly appreciated.

I need to:

1) loop through a JSON array (working)

2) display the first two results for "mbrname"

3) then display a count for the rest of the results.

I am successfully looping through and displaying ALL mbrname results. But somehow I need to only display the first two and if there are more display "+ # others"

Here's a screenshot of what the final product should look like: enter image description here

Here's what my code produces now: enter image description here

Here's my JSON:

{
 "messages":{
  "message":[
     {
        "date-time":"June 2, 2013 12:22 pm",
        "subject":"This is the message subject",
        "msg-string":"001",
        "newmsg":"true",
        "attach":"shop-cart",
        "recipient":[
           {
              "mbrname":"D. Craig",
              "mbr-href":"#craig"
           },
           {
              "mbrname":"N. McCoy",
              "mbr-href":"#mccoy"
           },
           {
              "mbrname":"J. Smith",
              "mbr-href":"#smith"
           },
           {
              "mbrname":"B. Wardlaw",
              "mbr-href":"#wardlaw"
           }
        ]
     },
     {
        "date-time":"May 23, 2013 12:22 pm",
        "subject":"This is a great subject",
        "attach":"none",
        "msg-string":"002",
        "newmsg":"true",
        "recipient":[
           {
              "mbrname":"D. Craig",
              "mbr-href":"#craig"
           },
           {
              "mbrname":"N. McCoy",
              "mbr-href":"#mccoy"
           }
        ]
     },
     {
        "date-time":"May 11, 2013 12:22 pm",
        "subject":"Interested in your tomatoes",
        "attach":"shop-cart",
        "msg-string":"003",
        "newmsg":"false",
        "recipient":[
           {
              "mbrname":"J. Smith",
              "mbr-href":"#smith"
           }
        ]
     }
  ]
 }
}

Here's my jquery just for the "mbrname" section which successfully pulls in the names and appends them to my HTML:

$.each (message.recipient, function (message, recipient) {
var mbrPart = recipient.mbrname + ', ';
var currentUser = $('#' + newID + ' .name');

$(currentUser).append(mbrPart);

});

Thanks in advance for any help!

2 Answers 2

6

I'd keep it simple, no need to do any looping:

var recipientString = message.recipient[0].mbrname;
var count = message.recipient.length;

if (count > 1)
    recipientString += ', ' + message.recipient[1].mbrname;

if (count > 2)
    recipientString += ' + ' + (count - 2) + ' others';

$('#' + newID + ' .name').append(recipientString);
Sign up to request clarification or add additional context in comments.

7 Comments

Yep. jQuery and $.each is all fancy, but sometimes, not using it results in better solutions.
better answer - +1 for you too sweety!
Yea I switched from my answer too :)
Thanks for your help. UNfortunately this breaks and in firebug I see this error: "ReferenceError: invalid assignment left-hand side" on if (count > 1) recipientString += ', ' = message.recipient[1].mbrname;
@NicoleMcCoy: = should have been +.
|
1

Something like this should work.

var count = 0;
$.each (message.recipient, function (message, recipient) {


if(count<2){
var mbrPart = recipient.mbrname + ', ';
var currentUser = $('#' + newID + ' .name');
$(currentUser).append(mbrPart);
}
count++;
});

$(currentUser).append(" + " + count-2 + " Others");

3 Comments

just beat me to it - for efficiency move var mbrPart inside the if(count) part so not initialising the var each time when not needed. +1
I like Jason's answer better now.
not sure if this is what you want, you'll end up with A. Craig, B.McCoy + 0 Others if you pass in exactly two recipients.

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.