{"id":8338,"date":"2020-07-11T16:11:37","date_gmt":"2020-07-11T16:11:37","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8338"},"modified":"2020-07-11T16:11:38","modified_gmt":"2020-07-11T16:11:38","slug":"destructing-in-javascript","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/destructing-in-javascript\/","title":{"rendered":"Destructing in JavaScript"},"content":{"rendered":"<p>In JavaScript\/TypeScript, if you&#8217;re using the <a href=\"https:\/\/eslint.org\/docs\/4.0.0\/rules\/prefer-destructuring\" rel=\"noopener noreferrer\" target=\"_blank\">Prefer destructuring from arrays and objects (prefer-destructuring)<\/a> eslint rule, you&#8217;ll want to use destructing syntax to get values from objects and arrays.<\/p>\n<p>If we imagine we have an object like this<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nclass Person\r\n{\r\n   firstName: string;\r\n   lastName: string;\r\n}\r\n<\/pre>\n<p>The to get the <em>firstName<\/em> from a Person instance, we tend to use<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst firstName = person.firstName;\r\n<\/pre>\n<p>Instead this rule prefers that we use the following syntax<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst { firstName } = person;\r\n<\/pre>\n<p>If for some reason (for example in React if you&#8217;re destructing state which may have been changed) you have need to get the value using destructing syntax but assigned to a new variable\/value name, then we use <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst { firstName: fname } = person;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript\/TypeScript, if you&#8217;re using the Prefer destructuring from arrays and objects (prefer-destructuring) eslint rule, you&#8217;ll want to use destructing syntax to get values from objects and arrays. If we imagine we have an object like this class Person { firstName: string; lastName: string; } The to get the firstName from a Person instance, we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[45,46],"tags":[],"class_list":["post-8338","post","type-post","status-publish","format-standard","hentry","category-javascript","category-typescript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8338","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8338"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8338\/revisions"}],"predecessor-version":[{"id":8421,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8338\/revisions\/8421"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}