{"id":679,"date":"2024-07-30T19:59:12","date_gmt":"2024-07-30T17:59:12","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=679"},"modified":"2024-07-30T19:59:13","modified_gmt":"2024-07-30T17:59:13","slug":"008-parametros-rest-operador-spread","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/04-estructuras-de-control-en-javascript\/008-parametros-rest-operador-spread\/","title":{"rendered":"008. Par\u00e1metros REST &amp; Operador Spread"},"content":{"rendered":"\n<p>En este cap\u00edtulo vamos a ver dos caracter\u00edsticas muy \u00fatiles, sobre todo si estamos metidos en el mundo, o si nuestra intenci\u00f3n posteriormente de aprender <strong>Javascript<\/strong> es aprender acerca de los <strong>Frameworks<\/strong> o librer\u00edas reactivas como <a href=\"https:\/\/angular.io\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Angular<\/strong>,<\/a> <a href=\"https:\/\/vuejs.org\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Vue<\/strong><\/a> o <a href=\"https:\/\/es.react.dev\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>React<\/strong>.<\/a> Los <strong>par\u00e1metros REST<\/strong> y el <strong>operador de propagaci\u00f3n<\/strong> o <strong>Spread Operator<\/strong> son herramientas que utilizamos en el d\u00eda a d\u00eda.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Par\u00e1metros REST<\/h2>\n\n\n\n<p>Los <strong>par\u00e1metros REST<\/strong> son una forma de virtualmente ir agregando par\u00e1metros infinitos ya sea a una <strong>funci\u00f3n<\/strong>, o bien dentro de una <strong>variable<\/strong>, que puede ser un <strong>array<\/strong>, pero no sabemos exactamente cuantos valores vamos a recibir, y posteriormente con esos valores vamos a realizar ciertas operaciones o ciertos procesos.<\/p>\n\n\n\n<p>Para definir los <strong>par\u00e1metros<\/strong> \u00fanicamente hay que anteponer <strong>3 puntos suspensivos<\/strong> (&#8230;) antes del nombre donde est\u00e1n guardados esos posibles valores infinitos.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"es\"&gt;\n&nbsp; &lt;head&gt;\n&nbsp; &nbsp; &lt;meta charset=\"UTF-8\" \/&gt;\n&nbsp; &nbsp; &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" \/&gt;\n&nbsp; &nbsp; &lt;title&gt;Par\u00e1metros REST&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\n\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;Par\u00e1metros REST&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; function sumar(a, b, ...c) {\n&nbsp; &nbsp; &nbsp; &nbsp; let resultado = a + b;\n&nbsp; &nbsp; &nbsp; &nbsp; c.forEach(function (n) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultado += n;\n&nbsp; &nbsp; &nbsp; &nbsp; });\n&nbsp; &nbsp; &nbsp; &nbsp; return resultado;\n&nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; console.log(sumar(1, 2));\n&nbsp; &nbsp; &nbsp; console.log(sumar(1, 2, 4));\n&nbsp; &nbsp; &nbsp; console.log(sumar(1, 2, 4, 7));\n&nbsp; &nbsp; &lt;\/script&gt;\n&nbsp; &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Operador de propagaci\u00f3n o spread operator<\/h2>\n\n\n\n<p>El <strong>operador de propagaci\u00f3n<\/strong> o <strong>spread operator<\/strong>, lo que hace es cuando una expresi\u00f3n la tengamos que expandir en situaciones donde tengamos que almacenar m\u00faltiples <strong>argumentos<\/strong> o <strong>elementos<\/strong>, lo podamos hacer. Un caso muy \u00fatil, es cuando tenemos un <strong>array<\/strong> con cierto n\u00famero de elementos, pero en cierto momento recibimos nuevos par\u00e1metros, entonces, en vez de estar haciendo ciertas <strong>concatenaciones<\/strong> o <strong>push<\/strong> a nuestro <strong>array<\/strong>, lo que hacemos es agregarlo con el <strong>spread operator<\/strong>, El <strong>spread operator<\/strong>, al igual que los <strong>par\u00e1metros REST<\/strong>,&nbsp; se distinguen por anteponer los <strong>3 puntos<\/strong> (&#8230;).<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"es\"&gt;\n&nbsp; &lt;head&gt;\n&nbsp; &nbsp; &lt;meta charset=\"UTF-8\" \/&gt;\n&nbsp; &nbsp; &lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" \/&gt;\n&nbsp; &nbsp; &lt;title&gt;Spread Operator&lt;\/title&gt;\n&nbsp; &lt;\/head&gt;\n\n&nbsp; &lt;body&gt;\n&nbsp; &nbsp; &lt;h1&gt;Spread Operator&lt;\/h1&gt;\n\n&nbsp; &nbsp; &lt;script&gt;\n&nbsp; &nbsp; &nbsp; const arr1 = [1, 2, 3, 4, 5],\n&nbsp; &nbsp; &nbsp; &nbsp; arr2 = [5, 6, 7, 8, 9, 0];\n&nbsp; &nbsp; &nbsp; console.log(arr1, arr2);\n&nbsp; &nbsp; &nbsp; \/\/ Crear un tercer array a partir de los dos creados usando spread operator\n&nbsp; &nbsp; &nbsp; const arr3 = [...arr1, ...arr2];\n&nbsp; &nbsp; &nbsp; console.log(arr3);\n&nbsp; &nbsp; &lt;\/script&gt;\n&nbsp; &lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>En este cap\u00edtulo vamos a ver dos caracter\u00edsticas muy \u00fatiles, sobre todo si estamos metidos en el mundo, o si [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":658,"menu_order":7,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"class_list":["post-679","page","type-page","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Sutil Web","author_link":"https:\/\/sutilweb.eu\/index.php\/author\/sutilweb\/"},"uagb_comment_info":0,"uagb_excerpt":"En este cap\u00edtulo vamos a ver dos caracter\u00edsticas muy \u00fatiles, sobre todo si estamos metidos en el mundo, o si [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/comments?post=679"}],"version-history":[{"count":2,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/679\/revisions"}],"predecessor-version":[{"id":681,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/679\/revisions\/681"}],"up":[{"embeddable":true,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/658"}],"wp:attachment":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/media?parent=679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}