Foros del Web » Programando para Internet » Javascript »

encontrar elemento en un array

Estas en el tema de encontrar elemento en un array en el foro de Javascript en Foros del Web. hola: tengo el siguientes js: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código javascript : Ver original function validate_exist ( lista , id_seach )             ...
  #1 (permalink)  
Antiguo 09/06/2009, 12:40
Avatar de destor77  
Fecha de Ingreso: noviembre-2004
Ubicación: Gálvez, Santa Fe, Argentina
Mensajes: 2.654
Antigüedad: 19 años, 5 meses
Puntos: 43
encontrar elemento en un array

hola:
tengo el siguientes js:
Código javascript:
Ver original
  1. function validate_exist(lista,id_seach)
  2.                     {
  3.                         var ind;
  4.                         for(ind=0; ind<lista.length; ind++)
  5.                            {
  6.                             if (lista[ind] == id_seach)
  7.                                 {
  8.                                   return true;
  9.                                 }
  10.                              else
  11.                                  {
  12.                                   return false;
  13.                                  }
  14.                               break;
  15.                             }
  16.                     }
  17.                     function filter_selected(obj)
  18.                     {
  19.                         location_array = new Array();
  20.                         country = new Array();
  21.                         province = new Array();
  22.                         city = new Array();        
  23.                     {/literal}                     
  24.                                                                        
  25.                     {section name=i loop=$location}
  26.                         location_array[{$smarty.section.i.index}]= new Array();
  27.                         location_array[{$smarty.section.i.index}]["country"]='{$location[i].country}';
  28.                         location_array[{$smarty.section.i.index}]["city"]='{$location[i].city}';
  29.                         location_array[{$smarty.section.i.index}]["province"]='{$location[i].province}';
  30.                         location_array[{$smarty.section.i.index}]["id"]='{$location[i].id}';
  31.                         location_array[{$smarty.section.i.index}]["name"]="{$location[i].name}";                                                                                                                               
  32.                     {/section}
  33.                     {section name=i loop=$country}
  34.                         country[{$smarty.section.i.index}]= new Array();
  35.                         country[{$smarty.section.i.index}]["id"]={$country[i].id};
  36.                         country[{$smarty.section.i.index}]["name"]="{$country[i].name}";
  37.                     {/section}
  38.                     {section name=i loop=$province}
  39.                         province[{$smarty.section.i.index}]= new Array();
  40.                         province[{$smarty.section.i.index}]["id"]={$province[i].id};
  41.                         province[{$smarty.section.i.index}]["name"]="{$province[i].name}";
  42.                     {/section}
  43.                     {section name=i loop=$city}
  44.                         city[{$smarty.section.i.index}]= new Array();
  45.                         city[{$smarty.section.i.index}]["id"]={$city[i].id};
  46.                         city[{$smarty.section.i.index}]["name"]="{$city[i].name}";
  47.                     {/section}                 
  48.                     {literal}                  
  49.                         if(obj.id=="country")
  50.                         {
  51.                             if(obj.value!="0"){
  52.                                 province_new = new Array();
  53.                                 city_new = new Array();
  54.                                 location_new = new Array();                            
  55.                                 for(i=0; i<location_array.length; i++)
  56.                                 {
  57.                                     loc=location_array[i];
  58.                                     if(loc["country"]==obj.value )
  59.                                     {
  60.                                         for(j=0; j<province.length; j++)
  61.                                         {
  62.                                             pro=province[j];
  63.                                             if(pro["id"]==loc["province"])
  64.                                             {
  65.                                               if(!validate_exist(province[j],loc["province"]))
  66.                                                     province_new.push(pro);
  67.                                             }
  68.                                                
  69.                                         }
  70.                                         for(j=0; j<city.length; j++)
  71.                                         {
  72.                                             cit=city[j];
  73.                                             if(cit["id"]==loc["city"])
  74.                                                 city_new.push(cit);
  75.                                         }
  76.                                         for(j=0; j<location_array.length; j++)
  77.                                         {
  78.                                             loc1=location_array[j];
  79.                                             if(loc1["id"]==loc["id"])
  80.                                                 location_new.push(loc1);
  81.                                         }                                      
  82.                                     }
  83.                                 }
  84.                                 province_html="<option value='0'>Geen voorkeur</option>";
  85.                                 city_html="<option value='0'>Geen voorkeur</option>";
  86.                                 location_html="<option value='0'>Geen voorkeur</option>";
  87.                                 for(j=0; j<province_new.length; j++)
  88.                                 {
  89.                                     pro=province_new[j];
  90.                                     province_html+="<option value='"+pro['id']+"'>"+pro['name']+"</option>";
  91.                                 }
  92.                                 document.getElementById("province").innerHTML=province_html;
  93.  
  94.                                 for(j=0; j<city_new.length; j++)
  95.                                 {
  96.                                     cit=city_new[j];
  97.                                     city_html+="<option value='"+cit['id']+"'>"+cit['name']+"</option>";
  98.                                 }
  99.                                 for(j=0; j<location_new.length; j++)
  100.                                 {
  101.                                     lo=location_new[j];
  102.                                     location_html+="<option value='"+lo['id']+"'>"+lo['name']+"</option>";
  103.                                 }
  104.  
  105.                                 document.getElementById("city").innerHTML=city_html;
  106.                                 document.getElementById("location_id").innerHTML=location_html;
  107.                                
  108.                                
  109.                             }
  110.                             else
  111.                             {
  112.                                 province_html="<option value='0'>Geen voorkeur</option>";
  113.                                 city_html="<option value='0'>Geen voorkeur</option>";
  114.                                 location_html="<option value='0'>Geen voorkeur</option>";
  115.                                 for(j=0; j<province.length; j++)
  116.                                 {
  117.                                     pro=province[j];
  118.                                     province_html+="<option value='"+pro['id']+"'>"+pro['name']+"</option>";
  119.                                 }
  120.                                 document.getElementById("province").innerHTML=province_html;
  121.                                 for(j=0; j<city.length; j++)
  122.                                 {
  123.                                     cit=city[j];
  124.                                     city_html+="<option value='"+cit['id']+"'>"+cit['name']+"</option>";
  125.                                 }
  126.                                 for(j=0; j<location_array.length; j++)
  127.                                 {
  128.                                     lo=location_array[j];
  129.                                     location_html+="<option value='"+lo['id']+"'>"+lo['name']+"</option>";
  130.                                 }
  131.                                 document.getElementById("city").innerHTML=city_html;
  132.                                 document.getElementById("location_id").innerHTML=location_html;                                
  133.                             }  
  134.                         }
  135.                        
  136.  
  137.                         if(obj.id=="province")
  138.                         {
  139.                             if(obj.value!="0"){
  140.                                 city_new = new Array();
  141.                                 location_new = new Array();                            
  142.                                 for(i=0; i<location_array.length; i++)
  143.                                 {
  144.                                     loc=location_array[i];
  145.                                     if(loc["province"]==obj.value )
  146.                                     {
  147.                                         for(j=0; j<city.length; j++)
  148.                                         {
  149.                                             cit=city[j];
  150.                                             if(cit["id"]==loc["city"])
  151.                                                 city_new.push(cit);
  152.                                         }
  153.                                         for(j=0; j<location_array.length; j++)
  154.                                         {
  155.                                             loc1=location_array[j];
  156.                                             if(loc1["id"]==loc["id"])
  157.                                                 location_new.push(loc1);
  158.                                         }                                      
  159.                                     }
  160.                                 }
  161.                                 city_html="<option value='0'>Geen voorkeur</option>";
  162.                                 location_html="<option value='0'>Geen voorkeur</option>";
  163.                                 for(j=0; j<city_new.length; j++)
  164.                                 {
  165.                                     cit=city_new[j];
  166.                                     city_html+="<option value='"+cit['id']+"'>"+cit['name']+"</option>";
  167.                                 }
  168.                                 for(j=0; j<location_new.length; j++)
  169.                                 {
  170.                                     lo=location_new[j];
  171.                                     location_html+="<option value='"+lo['id']+"'>"+lo['name']+"</option>";
  172.                                 }
  173.                                 document.getElementById("city").innerHTML=city_html;
  174.                                 document.getElementById("location_id").innerHTML=location_html;
  175.                             }
  176.                             else
  177.                             {
  178.                                 city_html="<option value='0'>Geen voorkeur</option>";
  179.                                 location_html="<option value='0'>Geen voorkeur</option>";
  180.                                 for(j=0; j<city.length; j++)
  181.                                 {
  182.                                     cit=city[j];
  183.                                     city_html+="<option value='"+cit['id']+"'>"+cit['name']+"</option>";
  184.                                 }
  185.                                 for(j=0; j<location_array.length; j++)
  186.                                 {
  187.                                     lo=location_array[j];
  188.                                     location_html+="<option value='"+lo['id']+"'>"+lo['name']+"</option>";
  189.                                 }
  190.                                 document.getElementById("city").innerHTML=city_html;
  191.                                 document.getElementById("location_id").innerHTML=location_html;                                
  192.                             }  
  193.                         }
  194.  
  195.                         if(obj.id=="city")
  196.                         {
  197.                             if(obj.value!="0"){
  198.                                 location_new = new Array();                            
  199.                                 for(i=0; i<location_array.length; i++)
  200.                                 {
  201.                                     loc=location_array[i];
  202.                                     if(loc["city"]==obj.value )
  203.                                     {
  204.                                         for(j=0; j<location_array.length; j++)
  205.                                         {
  206.                                             loc1=location_array[j];
  207.                                             if(loc1["id"]==loc["id"])
  208.                                                 location_new.push(loc1);
  209.                                         }                                      
  210.                                     }
  211.                                 }
  212.                                 location_html="<option value='0'>Geen voorkeur</option>";
  213.                                 for(j=0; j<location_new.length; j++)
  214.                                 {
  215.                                     lo=location_new[j];
  216.                                     location_html+="<option value='"+lo['id']+"'>"+lo['name']+"</option>";
  217.                                 }
  218.                                 document.getElementById("location_id").innerHTML=location_html;
  219.                             }
  220.                             else
  221.                             {
  222.                                 location_html="<option value='0'>Geen voorkeur</option>";
  223.                                 for(j=0; j<location_array.length; j++)
  224.                                 {
  225.                                     lo=location_array[j];
  226.                                     location_html+="<option value='"+lo['id']+"'>"+lo['name']+"</option>";
  227.                                 }
  228.                                 document.getElementById("location_id").innerHTML=location_html;                                
  229.                             }  
  230.                         }
  231.  
  232.                        
  233.                     }

lo que necesito hacer es que antes de que agregue cada nuevo elemento al array pregunte si no existe previamente en caso de que exista que no lo agregue, intente con el codigo de arriba, pero no funciona :s no me hace nada

alguna idea para darme?
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:11.