{"id":553,"date":"2017-06-28T00:49:49","date_gmt":"2017-06-28T01:19:49","guid":{"rendered":"http:\/\/www.mind4y.co\/itstuff\/2017\/06\/28\/javascript-arrays-reducing-to-an-array\/"},"modified":"2020-03-19T23:31:20","modified_gmt":"2020-03-20T00:01:20","slug":"javascript-arrays-reducing-to-an-array","status":"publish","type":"post","link":"https:\/\/ultering.com\/it4us\/?p=553","title":{"rendered":"JAVASCRIPT: ARRAYS &#8211; REDUCING TO AN ARRAY"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\n\/\/**\r\n\t* Function that recieves a array and the rule to filter the itens on it\r\n\t* One sample:\r\n\t* function reduceToArray([],item=&gt;item.id==1);\r\n\t*\r\n\t* Rule is to be a function that will have a item of the array as the parameter and his result is a boolean\r\n\t* like this for example:\r\n\t* function rule(item){\r\n\t* return item.id ==1;\r\n\t* }\r\n\t* @Author Leonardocregis\r\n\t*\/\r\n\r\n\tfunction reduceToArray (arr,rule){\r\n\tif ( Array.isArray(arr)){\r\n\tif (arr.length {\r\n\tif ( !Array.isArray(acc)){\r\n\tlet aux = acc ;\r\n\tacc = [];\r\n\tif (rule(aux)){\r\n\tacc.push(aux);\r\n\t}\r\n\t}\r\n\tif (rule(curr))\r\n\tacc.push(curr);\r\n\treturn acc;\r\n\t});\r\n\t}\r\n\tif (rule(arr)){\r\n\tlet newArr = [];\r\n\tnewArr.push(arr);\r\n\treturn newArr;\r\n\t}\r\n\treturn [];\r\n\t};\r\n\r\n<\/pre>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\/\/** * Function that recieves a array and the rule to filter the itens on it * One sample: * function reduceToArray([],item=&gt;item.id==1); * * Rule is to be a function that will have a item of the array as the parameter and his result is a boolean * like this for example: * function rule(item){ &#8230; <a href=\"https:\/\/ultering.com\/it4us\/?p=553\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &#8220;JAVASCRIPT: ARRAYS &#8211; REDUCING TO AN ARRAY&#8221;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[26],"tags":[7,8,9,10,11],"class_list":["post-553","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-array","tag-filter","tag-filtering","tag-javascript","tag-reduce"],"_links":{"self":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=553"}],"version-history":[{"count":3,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":2656,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/553\/revisions\/2656"}],"wp:attachment":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}