{"id":1201,"date":"2013-05-07T22:34:55","date_gmt":"2013-05-08T04:34:55","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=1201"},"modified":"2018-07-01T23:20:01","modified_gmt":"2018-07-02T05:20:01","slug":"code-of-the-day-javascript-flatten","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2013\/05\/code-of-the-day-javascript-flatten\/","title":{"rendered":"Code of the day: Javascript Flatten()"},"content":{"rendered":"<pre class=\"prettyprint linenums lang-javascript\">\r\n\/**\r\n* Returns an array that contains non-array elements.\r\n* If an element is an array, then the element is replaced by the content of the array.\r\n* @param{Array} \r\n* @return{Array} return null if no arguments are passed.\r\n* @example \r\n    flatten([[1,2],3,[4]]); \/\/ returns [1,2,3,4];\r\n*\/\r\nvar flatten = function(arr){\r\n\tif(!Array.isArray(arr)){\r\n\t\treturn (arr === null || arr === undefined) ? null : [arr];\r\n\t}\r\n\tvar result = [], obj;\r\n\tfor(var i = 0, len = arr.length; i < len; i++){\r\n\t\tobj = arr[i];\r\n\t\tif(Array.isArray(arr)){\r\n\t\t\tobj = flatten(obj);\r\n\t\t}\r\n\t\tresult = result.concat( obj );\r\n\t}\r\n    return result;\r\n};\r\n<\/pre>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"300\" src=\"\/\/jsfiddle.net\/Z2k7t\/1\/embedded\/\" allowfullscreen=\"allowfullscreen\" frameborder=\"0\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/** * Returns an array that contains non-array elements. * If an element is an array, then the element is replaced by the content of the array. * @param{Array} * @return{Array} return null if no arguments are passed. * @example flatten([[1,2],3,[4]]); \/\/ returns [1,2,3,4]; *\/ var flatten = function(arr){ if(!Array.isArray(arr)){ return (arr === null || &hellip; <a href=\"https:\/\/bateru.com\/news\/2013\/05\/code-of-the-day-javascript-flatten\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the day: Javascript Flatten()<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[30,164],"class_list":["post-1201","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","tag-code-of-the-day","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=1201"}],"version-history":[{"count":5,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1201\/revisions"}],"predecessor-version":[{"id":1586,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/1201\/revisions\/1586"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=1201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=1201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=1201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}