{"id":2928,"date":"2024-07-04T08:14:48","date_gmt":"2024-07-04T14:14:48","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=2928"},"modified":"2024-07-04T08:14:49","modified_gmt":"2024-07-04T14:14:49","slug":"new-javascript-set-methods","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/new-javascript-set-methods\/","title":{"rendered":"New JavaScript Set methods"},"content":{"rendered":"\n<p>Sets in JavaScript are quite useful as they are like an array, but can only contain <em>one<\/em> of a specific item. So you can unique-ify an array easily, and even willy-nilly add stuff to it and not worry about duplicates:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> people = &#91;<span class=\"hljs-string\">\"chris\"<\/span>, <span class=\"hljs-string\">\"chris\"<\/span>, <span class=\"hljs-string\">\"cindy\"<\/span>];\n<span class=\"hljs-keyword\">const<\/span> peopleSet = <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-built_in\">Set<\/span>(people);\npeopleSet.add(<span class=\"hljs-string\">\"chris\"<\/span>);\n<span class=\"hljs-built_in\">console<\/span>.log(peopleSet);\n<span class=\"hljs-comment\">\/\/ {\"chris\",\"cindy\"}<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now they are even more useful with recently universally available methods, <a href=\"https:\/\/developer.mozilla.org\/en-US\/blog\/javascript-set-methods\/\">as Brian Smith summarizes<\/a>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Set\/intersection\"><code>intersection()<\/code><\/a>\u00a0returns a new set with elements in both this set and the given set.<\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Set\/union\"><code>union()<\/code><\/a>\u00a0returns a new set with all elements in this set and the given set.<\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Set\/difference\"><code>difference()<\/code><\/a>\u00a0returns a new set with elements in this set but not in the given set.<\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Set\/symmetricDifference\"><code>symmetricDifference()<\/code><\/a>\u00a0returns a new set with elements in either set, but not in both.<\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Set\/isSubsetOf\"><code>isSubsetOf()<\/code><\/a>\u00a0returns a boolean indicating if all elements of a set are in a specific set.<\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Set\/isSupersetOf\"><code>isSupersetOf()<\/code><\/a>\u00a0returns a boolean indicating if all elements of a set are in a specific set.<\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Set\/isDisjointFrom\"><code>isDisjointFrom()<\/code><\/a>\u00a0returns a boolean indicating if this set has no elements in common with a specific set.<\/li>\n<\/ul>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Sets in JavaScript are quite useful as they are like an array, but can only contain one of a specific item. So you can unique-ify an array easily, and even willy-nilly add stuff to it and not worry about duplicates: Now they are even more useful with recently universally available methods, as Brian Smith summarizes:<\/p>\n","protected":false},"author":1,"featured_media":2931,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29],"tags":[3,196],"class_list":["post-2928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-the-beat","tag-javascript","tag-set"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/07\/czNmcy1wcml2YXRlL3Jhd3BpeGVsX2ltYWdlcy93ZWJzaXRlX2NvbnRlbnQvam9iNjc5LTEyOC1wLWwxNjd4Y2h3LnBuZw.jpg?fit=1300%2C867&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/2928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=2928"}],"version-history":[{"count":2,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/2928\/revisions"}],"predecessor-version":[{"id":2930,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/2928\/revisions\/2930"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/2931"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=2928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=2928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=2928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}