{"id":675,"date":"2021-07-06T12:27:41","date_gmt":"2021-07-06T10:27:41","guid":{"rendered":"http:\/\/strawberrycode.com\/blog\/?p=675"},"modified":"2021-07-06T12:27:43","modified_gmt":"2021-07-06T10:27:43","slug":"js-array-string","status":"publish","type":"post","link":"https:\/\/strawberrycode.com\/blog\/js-array-string\/","title":{"rendered":"Javascript Array to String and String to Array"},"content":{"rendered":"\n<p>Unfortunately for us, the methods to turn arrays to strings and strings to arrays are not named that explicitly, but here is a quick cheat sheet on how to do it.<\/p>\n\n\n\n<h2>Array to String<\/h2>\n\n\n\n<p>Let&#8217;s say we have an array of fruits: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let fruitsArray = ['strawberries', 'bananas', 'apples'];<\/pre>\n\n\n\n<p>To make it an array, we have 2 options: <code>toString()<\/code> and <code>join()<\/code>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let fruitsJoin = fruitsArray.join();\nconsole.log(\"fruitsJoin: %o\", fruitsJoin);\n\/\/ returns: strawberries,bananas,apples\n\nlet fruitsJoinDash = fruitsArray.join(\" - \");\nconsole.log(\"fruitsJoinDash: %o\", fruitsJoinDash);\n\/\/ returns: strawberries - bananas - apples\n\nlet fruitsToString = fruitsArray.toString();\nconsole.log(\"fruitsToString: %o\", fruitsToString);\n\/\/ returns: strawberries,bananas,apples<\/pre>\n\n\n\n<p>We can see in this example that <code>join()<\/code> without parameter is equivalent to <code>toString()<\/code>. For more flexibility, I prefer using <code>join()<\/code> to specify the <em>separator<\/em> I want (by default, it&#8217;s: <code>\",\"<\/code>). <\/p>\n\n\n\n<p>Have a look at the documentation for more details about <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/join\" target=\"_blank\" rel=\"noreferrer noopener\">join()<\/a> and <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/toString\" target=\"_blank\" rel=\"noreferrer noopener\">toString()<\/a>.<\/p>\n\n\n\n<h2>String to Array<\/h2>\n\n\n\n<p>Let&#8217;s say we have now a vegetables string: <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let vegetablesString = \"potatoes, broccoli, carrots\";<\/pre>\n\n\n\n<p>Now, to make it into an array, all we need to do is to split it!<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">let vegetablesSplit = vegetablesString.split(\", \");\nconsole.log(\"vegetablesSplit: %o\", vegetablesSplit);\n\/\/ returns: [\"potatoes\",\"broccoli\",\"carrots\"]\n\nlet vegetablesSplitDefault = vegetablesString.split();\nconsole.log(\"vegetablesSplitDefault: %o\", vegetablesSplitDefault);\n\/\/ returns: [\"potatoes, broccoli, carrots\"]\n\nlet vegetablesSplitLimit = vegetablesString.split(\", \", 2);\nconsole.log(\"vegetablesSplitLimit: %o\", vegetablesSplitLimit);\n\/\/ returns: [\"potatoes\",\"broccoli\"]<\/pre>\n\n\n\n<p>The <code>split()<\/code> method takes 2 optional parameters: the <em>separator<\/em> and the <em>limit<\/em>. Please note that if you don&#8217;t specify the <em>separator<\/em>, <code>split()<\/code> will return an array containing the entire original string. Also, the <em>limit<\/em>, as its name states, will limit the number of elements returned in the array. <\/p>\n\n\n\n<p>Have a look at the documentation for more details about <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\/split\" target=\"_blank\" rel=\"noreferrer noopener\">split()<\/a>.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>&#x1f449;As always, you can find all the examples above (and play with them) in the dedicated Codepen: <a href=\"https:\/\/codepen.io\/strawberrycode\/pen\/mdmVOYY\" target=\"_blank\" rel=\"noreferrer noopener\">JS Array to String and String to Array<\/a>.<\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Feel free to\u00a0<a href=\"https:\/\/www.buymeacoffee.com\/strawberrycode\" target=\"_blank\" rel=\"noreferrer noopener\">buy me a coffee (or tea)<\/a>\u00a0if you found this article useful\u00a0&#x2615;&#xfe0f; &#x1f642;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unfortunately for us, the methods to turn arrays to strings and strings to arrays are not named that explicitly, but here is a quick cheat sheet on how to do it. Array to String Let&#8217;s say we have an array of fruits: To make it an array, we have 2 options: toString() and join(). We can see in this example that join() without parameter is equivalent to toString(). For more flexibility, I prefer using join() to specify the separator I want (by default, it&#8217;s: &#8220;,&#8221;). Have a look at the documentation for more details about join() and toString(). String to &hellip; <a href=\"https:\/\/strawberrycode.com\/blog\/js-array-string\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Javascript Array to String and String to Array<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":679,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[115],"tags":[132,129,133],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Javascript Array to String and String to Array - StrawberryCode<\/title>\n<meta name=\"description\" content=\"Unfortunately for us, the methods to turn javascript arrays to strings and vice versa are not named that explicitly, but here are 3 methods do do so.\" \/>\n<link rel=\"canonical\" href=\"http:\/\/strawberrycode.com\/blog\/js-array-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Javascript Array to String and String to Array - StrawberryCode\" \/>\n<meta property=\"og:description\" content=\"Unfortunately for us, the methods to turn javascript arrays to strings and vice versa are not named that explicitly, but here are 3 methods do do so.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/strawberrycode.com\/blog\/js-array-string\/\" \/>\n<meta property=\"og:site_name\" content=\"StrawberryCode\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-06T10:27:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-06T10:27:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/07\/js_array_string.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1758\" \/>\n\t<meta property=\"og:image:height\" content=\"920\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/#website\",\"url\":\"https:\/\/strawberrycode.com\/blog\/\",\"name\":\"StrawberryCode\",\"description\":\"Fruit for Thought\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/strawberrycode.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/strawberrycode.com\/blog\/js-array-string\/#primaryimage\",\"inLanguage\":\"en-GB\",\"url\":\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/07\/js_array_string.png\",\"contentUrl\":\"https:\/\/strawberrycode.com\/blog\/wp-content\/uploads\/2021\/07\/js_array_string.png\",\"width\":1758,\"height\":920,\"caption\":\"JS array to string - string to array\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/strawberrycode.com\/blog\/js-array-string\/#webpage\",\"url\":\"http:\/\/strawberrycode.com\/blog\/js-array-string\/\",\"name\":\"Javascript Array to String and String to Array - StrawberryCode\",\"isPartOf\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/strawberrycode.com\/blog\/js-array-string\/#primaryimage\"},\"datePublished\":\"2021-07-06T10:27:41+00:00\",\"dateModified\":\"2021-07-06T10:27:43+00:00\",\"author\":{\"@id\":\"https:\/\/strawberrycode.com\/blog\/#\/schema\/person\/c328d959959928f47281d7a0ec779e2a\"},\"description\":\"Unfortunately for us, the methods to turn javascript arrays to strings and vice versa are not named that explicitly, but here are 3 methods do do so.\",\"breadcrumb\":{\"@id\":\"http:\/\/strawberrycode.com\/blog\/js-array-string\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/strawberrycode.com\/blog\/js-array-string\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/strawberrycode.com\/blog\/js-array-string\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/\",\"url\":\"https:\/\/strawberrycode.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"http:\/\/strawberrycode.com\/blog\/js-array-string\/\",\"url\":\"http:\/\/strawberrycode.com\/blog\/js-array-string\/\",\"name\":\"Javascript Array to String and String to Array\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/strawberrycode.com\/blog\/#\/schema\/person\/c328d959959928f47281d7a0ec779e2a\",\"name\":\"StrawberryCode\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/675"}],"collection":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/comments?post=675"}],"version-history":[{"count":2,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/675\/revisions"}],"predecessor-version":[{"id":678,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/posts\/675\/revisions\/678"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/media\/679"}],"wp:attachment":[{"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/media?parent=675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/categories?post=675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/strawberrycode.com\/blog\/wp-json\/wp\/v2\/tags?post=675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}