{"id":120,"date":"2022-11-17T17:04:51","date_gmt":"2022-11-17T17:04:51","guid":{"rendered":"http:\/\/javigomez.org\/?p=120"},"modified":"2025-09-29T20:34:30","modified_gmt":"2025-09-29T20:34:30","slug":"filtrar-un-array-en-javascript","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2022\/11\/17\/filtrar-un-array-en-javascript\/","title":{"rendered":"Filtrar un array en Javascript"},"content":{"rendered":"\n<p>Se introduce un campo con textos separados con comas y se convierte en una tabla con la funci\u00f3n split. A continuaci\u00f3n se busca filtra ese array con una palabra.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"http:\/\/javigomez.org\/wp-content\/uploads\/2022\/11\/image-29-1-17-1.png\" alt=\"\" class=\"wp-image-121\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n    &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    &lt;title>4.8&lt;\/title>\n&lt;\/head>\n\n&lt;body>\n    &lt;h1>4.8 Una tabla que cumpla con el filtro introducido&lt;\/h1>\n    &lt;p>Introduce los primeros enteros separados por comas: &lt;\/p>\n    &lt;input type=\"text\" placeholder=\"Array\" id=\"array\">\n    &lt;p>\n    Valor a buscar: &lt;input type=\"text\" placeholder=\"Texto a a\u00f1adir\" id=\"valor\">\n    \n          &lt;input type=\"button\" value=\"Filtrar\" onclick=\"filtrar()\">\n    &lt;\/p>\n  \n    &lt;input type=\"button\" value=\"Reiniciar\" onclick=\"reiniciar()\">\n    &lt;p id=\"resultado\">&lt;\/p>\n    \n  &lt;script>\n  var array=&#91;], valor=\"\";\n\n  function filtrar()\n  {\n      array=document.getElementById(\"array\").value.split(\/,\/);\n      valor=document.getElementById(\"valor\").value;\n\n      var array2=array.filter(filtra);\n\n      array2.forEach(muestra);\n  }\nfunction filtra(ele){\n      var ret=false;\n      if (ele.includes(valor))\n            ret=true;\n      return ret;\n}\n\n  function muestra(ele,ind,arr){\n    document.getElementById(\"resultado\").innerHTML = document.getElementById(\"resultado\").innerHTML +\"Ind:\"+ ind + \" elemento: \" + ele + \"&lt;br>\";\n  }\n  function reiniciar()\n  {\n        location.reload();\n  }\n  \n  &lt;\/script>\n  &lt;\/body>\n\n&lt;\/html><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Se introduce un campo con textos separados con comas y se convierte en una tabla con la funci\u00f3n split. A continuaci\u00f3n se busca filtra ese &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[151,163,204,313],"class_list":["post-120","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-find","tag-foreach","tag-javascript","tag-split"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/comments?post=120"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/120\/revisions"}],"predecessor-version":[{"id":985,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/120\/revisions\/985"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}