{"id":674,"date":"2024-07-30T19:57:04","date_gmt":"2024-07-30T17:57:04","guid":{"rendered":"https:\/\/blog.sutilweb.eu\/?page_id=674"},"modified":"2024-07-30T19:57:05","modified_gmt":"2024-07-30T17:57:05","slug":"006-destructuracion","status":"publish","type":"page","link":"https:\/\/sutilweb.eu\/index.php\/lenguajes\/javascript\/javascript-practico\/04-estructuras-de-control-en-javascript\/006-destructuracion\/","title":{"rendered":"006. Destructuraci\u00f3n"},"content":{"rendered":"\n<p>La <strong>destructuraci\u00f3n<\/strong> o <strong>destructuring<\/strong> es una pr\u00e1ctica muy utilizada en <strong>Javascript<\/strong>, lleg\u00f3 con <strong>ES6<\/strong>, y es una nueva forma de asignar valores sobretodo a <strong>arrays<\/strong> y <strong>objetos<\/strong>. Si estamos en el mundo de <strong>Javascript<\/strong> utilizando librer\u00edas o <strong>framewords<\/strong> como <a href=\"https:\/\/angular.io\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Angular<\/strong>,<\/a> <a href=\"https:\/\/es.react.dev\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>React<\/strong><\/a> o <a href=\"https:\/\/vuejs.org\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Vue<\/strong>,<\/a> seguramente hayas utilizado este tipo de sintaxis en la cual asignas din\u00e1micamente lo que viene en un <strong>array<\/strong> o un <strong>objeto<\/strong> a nuevas variables de una manera m\u00e1s \u00e1gil.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Veamos un ejm con un <strong>array<\/strong>.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const numeros = [1, 2, 3];\n\n&nbsp; &nbsp; &nbsp; \/\/ Sin usar destructuraci\u00f3n\n&nbsp; &nbsp; &nbsp; let uno = numeros[0];\n&nbsp; &nbsp; &nbsp; let dos = numeros[1];\n&nbsp; &nbsp; &nbsp; let tres = numeros[2];\n&nbsp; &nbsp; &nbsp; console.log(uno, dos, tres);\n\n&nbsp; &nbsp; &nbsp; \/\/ Usando destructuring\n&nbsp; &nbsp; &nbsp; const [one, two, three] = numeros;\n&nbsp; &nbsp; &nbsp; console.log(one, two, three);<\/pre>\n\n\n\n<p>Veamos otro ejm con un <strong>objeto<\/strong>.<\/p>\n\n\n\n<p><strong>Ejm<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp; &nbsp; const persona = {\n&nbsp; &nbsp; &nbsp; &nbsp; nombre: \"Francisco\",\n&nbsp; &nbsp; &nbsp; &nbsp; apellido: \"Paredes\",\n&nbsp; &nbsp; &nbsp; &nbsp; edad: 51,\n&nbsp; &nbsp; &nbsp; };\n\n&nbsp; &nbsp; &nbsp; \/\/ Destructuramos persona\n&nbsp; &nbsp; &nbsp; const { nombre, apellido, edad } = persona;\n&nbsp; &nbsp; &nbsp; console.log(nombre, apellido, edad);<\/pre>\n\n\n\n<p>Para que la <strong>destructuraci\u00f3n<\/strong> funcione en los <strong>objetos<\/strong>, es muy importante que la <strong>variable<\/strong> que estamos creando con la t\u00e9cnica de <strong>destructuring<\/strong> tenga el mismo nombre que la <strong>propiedad<\/strong> que pretendemos aislar en una dicha variable, de lo contrario marcar\u00e1 un error. El orden en que pongamos las <strong>variables<\/strong> con esta t\u00e9cnica es opcional mientras que entendamos que el nombre de la <strong>variable<\/strong> que estamos creando debe ser igual al de la <strong>propiedad<\/strong> de nuestro <strong>objeto<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>La destructuraci\u00f3n o destructuring es una pr\u00e1ctica muy utilizada en Javascript, lleg\u00f3 con ES6, y es una nueva forma de [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":658,"menu_order":5,"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-674","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":"La destructuraci\u00f3n o destructuring es una pr\u00e1ctica muy utilizada en Javascript, lleg\u00f3 con ES6, y es una nueva forma de [&hellip;]","_links":{"self":[{"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/674","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=674"}],"version-history":[{"count":1,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/674\/revisions"}],"predecessor-version":[{"id":675,"href":"https:\/\/sutilweb.eu\/index.php\/wp-json\/wp\/v2\/pages\/674\/revisions\/675"}],"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=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}