www.webdeveloper.com
Results 1 to 4 of 4

Thread: How can I filtering array of objects against varaible parameters in javascript

  1. #1
    Join Date
    Feb 2009
    Posts
    95

    How can I filtering array of objects against varaible parameters in javascript

    I have an array like the following and I want to search only those values against gAssessorsCompanyAssociationsArray array where a user has entered something in any textbox and where any dropdown value is not "- Select One -". For example if a user has selected something in "Doctor Name" other than "- Select One -" then it should be part of my search against gAssessorsCompanyAssociationsArray array. If a user has entered something in "City" then it should be part of my search against gAssessorsCompanyAssociationsArray array.

    I am storing if statement's expression is Profile_Filter variable and executing it using eval(which is not recommended).

    My Array:
    Code:
    gAssessorsCompanyAssociationsArray[0] = new Object();   
    gAssessorsCompanyAssociationsArray[0].DoctorName = gAssessorsInfoArray[j].DoctorName;   
    gAssessorsCompanyAssociationsArray[0].DoctorLocation  = gAssessorsInfoArray[j].DoctorLocation;  
    gAssessorsCompanyAssociationsArray[0].Specialty = gAssessorsInfoArray[j].Specialty; 
    gAssessorsCompanyAssociationsArray[0].SubSpecialty = gAssessorsInfoArray[j].SubSpecialty;
    gAssessorsCompanyAssociationsArray[0].City = gAssessorsInfoArray[j].City;   
    
    gAssessorsCompanyAssociationsArray[1] = new Object();   
    gAssessorsCompanyAssociationsArray[1].DoctorName = gAssessorsInfoArray[j].DoctorName;   
    gAssessorsCompanyAssociationsArray[1].DoctorLocation  = gAssessorsInfoArray[j].DoctorLocation;  
    gAssessorsCompanyAssociationsArray[1].Specialty = gAssessorsInfoArray[j].Specialty; 
    gAssessorsCompanyAssociationsArray[1].SubSpecialty = gAssessorsInfoArray[j].SubSpecialty;
    gAssessorsCompanyAssociationsArray[1].City = gAssessorsInfoArray[j].City;
    My Code:
    Code:
    if($('#DoctorNameDropDown option:selected').text() != '- Select One -' || $('#DoctorLocationID').val() != '' || $('#SpecialtyDropDown option:selected').text() != '- Select One -' || $('#SubSpecialty').val() != '' || $('#City').val() != '')
            {               
                var Profile_Filter = '';
    
                if($('#DoctorNameDropDown option:selected').text() != '- Select One -')
                {                       
                    Profile_Filter += 'gAssessorsCompanyAssociationsArray[i].UserID == $("#DoctorNameDropDown option:selected").val()';                                                         
                }
    
                if($('#DoctorLocationID').val() != '' && $('#DoctorLocationID').val() != 0)
                {
                    if(Profile_Filter != '')                        
                        Profile_Filter += ' && ' + 'gAssessorsCompanyAssociationsArray[i].CompanyID == $("#DoctorLocationID").val()';
                    else
                        Profile_Filter += 'gAssessorsCompanyAssociationsArray[i].CompanyID == $("#DoctorLocationID").val()';
                }
    
                if($('#SpecialtyDropDown option:selected').text() != '- Select One -')
                {               
                    if(Profile_Filter != '')                        
                        Profile_Filter += ' && ' + 'gAssessorsCompanyAssociationsArray[i].AssessorSpecialty == $("#SpecialtyDropDown option:selected").val()';
                    else
                        Profile_Filter += 'gAssessorsCompanyAssociationsArray[i].AssessorSpecialty == $("#SpecialtyDropDown option:selected").val()';
                }
    
                if($('#SubSpecialty').val() != '' && $('#SubSpecialty').val() != 0)
                {                       
                    var SubSpecialty_Filter = '';
                    var SubSpecialty = $('#SubSpecialty').val().split(';');                         
    
                    for(var i = 0; i < SubSpecialty.length; i++)
                    {
                        if(SubSpecialty[i] == '')
                        {
                            SubSpecialty.splice(i, 1);
                            i--;                
                        }                                           
                    }                           
    
                    if(SubSpecialty.length > 1)
                    {                       
                        for(var j = 0; j < SubSpecialty.length; j++)
                        {
                            if(j == 0)
                            {
                                SubSpecialty_Filter += '(gAssessorsCompanyAssociationsArray[i].SubSpecialty.indexOf("' + SubSpecialty[j] + '") > -1';                                           
                            }
                            else
                            {
                                SubSpecialty_Filter += ' && gAssessorsCompanyAssociationsArray[i].SubSpecialty.indexOf("' + SubSpecialty[j] + '") > -1';                                        
                            }                                
                        }
                        SubSpecialty_Filter += ')';                     
                    }
                    else
                        SubSpecialty_Filter +=  '(gAssessorsCompanyAssociationsArray[i].SubSpecialty.indexOf("' + SubSpecialty[0] + '") > -1)'; 
    
                    if(Profile_Filter != '')                        
                        Profile_Filter += ' && ' + SubSpecialty_Filter; 
                    else
                        Profile_Filter += SubSpecialty_Filter;      
                }                       
    
                if($('#City').val() != '')
                {
                    if(Profile_Filter != '')                        
                        Profile_Filter += ' && ' + 'gAssessorsCompanyAssociationsArray[i].CompanyCity == $("#City").val()';
                    else
                        Profile_Filter += 'gAssessorsCompanyAssociationsArray[i].CompanyCity == $("#City").val()';
                }                   
    
                for(var i = 0; i < gAssessorsCompanyAssociationsArray.length; i++)
                {
                    if(eval(Profile_Filter))
                    {
                        gAssessorsIDSArray.push(gAssessorsCompanyAssociationsArray[i].UserID); 
                        gBookingsProfileArray.push(gAssessorsCompanyAssociationsArray[i]);                                      
                    }               
                }
        }
    My HTML:
    Code:
    <table>
                    <tbody>                  
                        <tr>                
                            <td> Doctor Name :</td>               
                            <td>
                                <select id='DoctorNameDropDown'>
                                    <option selected = '' value=''>- Select One -</option>
                                </select>
                            </td>
                            <td> Doctor Location :</td>
                            <td>                           
                                <div style="position:relative;height:100%;display:block;z-index:95;" id="DoctorLocationlookupdiv">
                                    <div onclick="this.style.display='none'" style="position:absolute;left:0;top:0;z-index:94;display:none;background:#FFFFFF" id="DoctorLocationResultdiv"></div>
                                    <input type="text" onkeyup="dyn_companysearch(event,this.value,document.getElementById('DoctorLocationResultdiv'),document.getElementById('DoctorLocationlookupdiv'),document.getElementById('DoctorLocationID'),this,1,102946,'1918')" autocomplete="off" class="Lookup" onblur="this.className='Lookup';setDynamicSearchValue(dynsearch_highlight_row);clearDynamicSearch(false);" style="width: 150px;"  id="DoctorLocationtxt">
                                </div>
                                <input type="hidden" id="DoctorLocationID" value="">                                
                            </td>               
                        </tr>           
    
                        <tr>                
                            <td> Specialty :</td>               
                            <td>                     
                                <select id="SpecialtyDropDown"><option selected = '' value=''>- Select One -</option><option value="Addictions">Addictions</option><option value="Anaesthesiology">Anaesthesiology</option></select>                        
                            </td>
                            <td> Sub Specialty :</td>
                            <td>                         
                                <input id="SubSpecialty" type="hidden" value="" name="SubSpecialty">
                                <div id="lookupdiv_1271728" class="ajaxLookUpContainer">
                                    <div id="resultdiv_1271728" class="ajaxLookUpResult" onclick="this.style.display='none'"></div>
                                    <input id="d_cf_1271728" class="Large" type="text" onkeyup="dyn_fieldoptionsearch(event,this.value,document.getElementById('resultdiv_1271728'),document.getElementById('lookupdiv_1271728'),document.getElementById('SubSpecialty'),this,1271728,1)" autocomplete="off" onblur="setDynamicSearchValue(dynsearch_highlight_row);clearDynamicSearch(false);" onclick="toTextBoxEnd(this)" value="" name="d_cf_1271728" size="100" title="" style="width: 150px;">
                                    <input type="hidden" value="WUZ2DVMBfUhGdEREV15LUF0DBnFae11bD1RTbDEDBgQ3Bws~" name="sec_cf_1271728">
                                </div>                              
                            </td>             
                        </tr>                       
                        <tr>                
                            <td> City :</td>                
                            <td>
                                <input type="text" id="City">  
                            </td>
                            <td></td>
                            <td>                             
                            </td>             
                        </tr>                                               
    
                        <tr>                        
                            <td colspan='4' align='center'> <input type='button' onclick='GetBookings();' value='Get Bookings'> </td> 
                        </tr>
                    </tbody>
                </table>

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,265
    Use the map method (see for exemple this MDN page), with somethig like this :

    Code:
    var nmb=[{fr:'zero',en:'zero'},{fr:'un',en:'one'},{fr:'deux',en:'two'},{fr:'trois',en:'three'},{fr:'quatre',en:'for'}];
    
    // Define a new array
    var evenNmb=[];
    // Use of the map function to select the elements satisfying the condition
    nmb.map(function(v,i){if (!(i%2)) evenNmb.push(v)});
    // The result
    alert(JSON.stringify(evenNmb))
    
    // To filter elements which have a t letter in english
    var tEnNmb=[];
    nmb.map(function(v,i){if (-1<v.en.indexOf('t')) tEnNmb.push(v)});
    alert(JSON.stringify(tEnNmb))
    The map method calls a provided callback function (here an anonymous function) once for each element in an array, in order, and constructs a new array from the results (this new array has the same length as the original). But we use It only to build the new array with elements satisfying the condition...
    The function is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed. We use only the two first arguments .
    Last edited by 007Julien; 11-14-2013 at 09:14 AM.

  3. #3
    Join Date
    Feb 2009
    Posts
    95
    Quote Originally Posted by 007Julien View Post
    Use the map method (see for exemple this MDN page), with somethig like this :

    Code:
    var nmb=[{fr:'zero',en:'zero'},{fr:'un',en:'one'},{fr:'deux',en:'two'},{fr:'trois',en:'three'},{fr:'quatre',en:'for'}];
    
    // Define a new array
    var evenNmb=[];
    // Use of the map function to select the elements satisfying the condition
    nmb.map(function(v,i){if (!(i%2)) evenNmb.push(v)});
    // The result
    alert(JSON.stringify(evenNmb))
    
    // To filter elements which have a t letter in english
    var tEnNmb=[];
    nmb.map(function(v,i){if (-1<v.en.indexOf('t')) tEnNmb.push(v)});
    alert(JSON.stringify(tEnNmb))
    The map method calls a provided callback function (here an anonymous function) once for each element in an array, in order, and constructs a new array from the results (this new array has the same length as the original). But we use It only to build the new array with elements satisfying the condition...
    The function is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed. We use only the two first arguments .
    Thanks I will take a look.

  4. #4
    Join Date
    Nov 2013
    Location
    victoria secret uk
    Posts
    1
    i like it, good ideas

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles