{"id":170,"date":"2022-12-01T07:35:49","date_gmt":"2022-12-01T07:35:49","guid":{"rendered":"http:\/\/javigomez.org\/?p=170"},"modified":"2025-09-29T20:34:20","modified_gmt":"2025-09-29T20:34:20","slug":"filtrado-de-un-array-en-javascript-array-filter","status":"publish","type":"post","link":"https:\/\/javigomez.org\/index.php\/2022\/12\/01\/filtrado-de-un-array-en-javascript-array-filter\/","title":{"rendered":"Filtrado de un array en Javascript. array.filter()"},"content":{"rendered":"\n<p>A trav\u00e9s de la funci\u00f3n filter() se puede realizar un filtrado de un array en Javascript. Esta funci\u00f3n es llamada por cada elemento del array, y si se realiza un return true, ese valor estar\u00e1 dentro del filtro, en caso contrario, ese valor no entrar\u00e1 dentro del filtro.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"383\" height=\"285\" src=\"http:\/\/javigomez.org\/wp-content\/uploads\/2022\/12\/image-14.png\" alt=\"\" class=\"wp-image-171\" srcset=\"https:\/\/javigomez.org\/wp-content\/uploads\/2022\/12\/image-14.png 383w, https:\/\/javigomez.org\/wp-content\/uploads\/2022\/12\/image-14-300x223.png 300w\" sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><\/figure><\/div>\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.9&lt;\/title>\n&lt;\/head>\n\n&lt;body>\n  &lt;h1>4.9 Saca los n\u00fameros pares&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\n    &lt;input type=\"button\" value=\"Pares\" onclick=\"pares()\">\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 pares() {\n      array = document.getElementById(\"array\").value.split(\/,\/);\n\n      var array2 = array.filter(filtra);\n\n      array2.forEach(muestra);\n    }\n\n\n    function filtra(ele) {\n      return !(parseInt(ele) % 2);\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      location.reload();\n    }\n\n  &lt;\/script>\n&lt;\/body>\n\n&lt;\/html><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A trav\u00e9s de la funci\u00f3n filter() se puede realizar un filtrado de un array en Javascript. Esta funci\u00f3n es llamada por cada elemento del array, &hellip; <\/p>\n","protected":false},"author":1,"featured_media":171,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[40,149,150,204,313],"class_list":["post-170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-array","tag-filter","tag-filtrado","tag-javascript","tag-split"],"_links":{"self":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/170","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=170"}],"version-history":[{"count":1,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/170\/revisions"}],"predecessor-version":[{"id":971,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/posts\/170\/revisions\/971"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media\/171"}],"wp:attachment":[{"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/media?parent=170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/categories?post=170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/javigomez.org\/index.php\/wp-json\/wp\/v2\/tags?post=170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}