Determine if JSON Object contains a value

I am creating a JSON object like this:
    window.attendees              = {};
    window.attendees.eventcode    = "GEVENT";
    window.attendees.price        = 169;
    window.attendees.WaitingList  = "";
    window.attendees.participants = [];

Open in new window


and populating it like this:
  function addParticipant() {
    var participant = {};
    // want to check here if the email to be added already exists in window.attendees.participants
    var valid = $('#checkout-step-billing .voucher_form').validationEngine('validate');
    if (valid) {
      participant.firstname   = $('#firstname').val();
      participant.lastname    = $('#lastname').val();
      participant.DOB         = $('#DOB').val();
      participant.Last4       = $('#Last4SSN').val();
      participant.PID         = $('#customer_id').val();
      participant.Phone       = $('#Phone').val();
      participant.Employer    = $('#Employer').val();
      participant.Address     = $('#Address').val();
      participant.City        = $('#City').val();
      participant.State       = $('#State').val();
      participant.Zip         = $('#Zip').val();
      participant.email       = $('#SomeoneElseEmail').val();
  
      window.attendees.participants.push(participant);
    } // if (valid)
  } // function addParticipant()

Open in new window


I need to prevent the same email address from being added twice if it already exists in window.attendees.participants.
LVL 26
Eddie ShipmanAll-around developerAsked:
Who is Participating?
 
leakim971PluritechnicianCommented:
function emailIsNotFound(arrParticipants, str) {
    for(var i=0;i<arrParticipants.length;i++) {
       if( arrParticipants[i]["email"] == str ) return false;
    }
    return true;
}

Open in new window


use :

  function addParticipant() {
    var participant = {};
    if( emailIsNotFound( window.attendees.participants, $('#SomeoneElseEmail').val() ) ) {
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.