Determine if JSON Object contains a value

Posted on 2011-10-04
Medium Priority
Last Modified: 2012-05-12
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();
    } // 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.
Question by:Eddie Shipman
1 Comment
LVL 83

Accepted Solution

leakim971 earned 2000 total points
ID: 36912449
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() ) ) {

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question