{"id":333,"date":"2013-05-10T18:57:40","date_gmt":"2013-05-10T18:57:40","guid":{"rendered":"http:\/\/www.webninjataylor.com\/library\/?p=333"},"modified":"2014-06-24T21:22:28","modified_gmt":"2014-06-24T21:22:28","slug":"underscore-js-basics","status":"publish","type":"post","link":"https:\/\/webninjataylor.com\/library\/underscore-js-basics\/","title":{"rendered":"Underscore.js Basics"},"content":{"rendered":"<p>Underscore.js is a tiny JavaScript utility library that makes working with some of the common data structures used in JavaScript much easier<\/p>\n<h2>Common Methods<\/h2>\n<ul>\n<li><span class=\"code\">_.union(myArray2, myArray3)<\/span> to concatenate two arrays<\/li>\n<li><span class=\"code\">_.uniq(myArray3)<\/span> to strip duplicates from array<\/li>\n<li><span class=\"code\">_.zip(myArray, myArray2)<\/span> to return arrays where the nth value of each array is paired [0, 0], [1, 1], [2, 2], etc&#8230;<\/li>\n<li><span class=\"code\">_.range(0,30,10)<\/span> to return an array of integers (start[optional], stop, increment[optional])<\/li>\n<li><span class=\"code\">_.size(myCollection)<\/span> to return the number of items in a collection {key: value, key: value}<\/li>\n<li><span class=\"code\">_.pluck(myCollection2, &#8220;key&#8221;)<\/span> returns an array of values from a collection\u00a0{key: value, key: value}<\/li>\n<li>Filter &#8220;key: value&#8221; sets out of a collection which don&#8217;t match certain criteria\n<pre><code>_.filter(myCollection2, function (item) {\r\n   return item.name.indexOf(\"a\") !== -1;\r\n});<\/code><\/pre>\n<\/li>\n<li><span class=\"code\">_.reject()<\/span>\u00a0is the polar opposite of filter<\/li>\n<li><span class=\"code\">_.shuffle(myArray)<\/span> to randomly shuffle an array<\/li>\n<li>Sort &#8220;key: value&#8221; sets in a collection\n<pre><code>_.sortBy(myCollection3, function (item) {\r\n   return item.name;\r\n});<\/code><\/pre>\n<\/li>\n<li><span class=\"code\">_.bind(function, object, [*arguments])<\/span> to bind a function to an object, meaning that whenever the function is called, the value of <span class=\"code\"><strong>this<\/strong><\/span> will be the object<\/li>\n<li><span class=\"code\">_.map()<\/span> to process an array into a new array\n<pre><code>var inputArray = ['apple', 'banana', 'pineapple'];\r\nvar resultArray = _.map(inputArray, function(fruit){\r\n    return fruit + 'man';\r\n});\r\n\/\/ resultArray = ['appleman','bananaman','pineappleman']\r\n\r\n\/\/ ... OR ...\r\n\r\nvar createFruitMan = function(fruit){\r\n    return fruit + 'man';\r\n};\r\nvar inputArray = ['apple', 'banana', 'pineapple'];\r\nvar resultArray = _.map(inputArray, createFruitMan);<\/code><\/pre>\n<\/li>\n<li><span class=\"code\">_.reduce(an input array to reduce, a function to call for each array element, and a start value for the memory element)<\/span>\u00a0to process an array into a string (<a href=\"http:\/\/jsfiddle.net\/webninjataylor\/QJ2qs\/\" target=\"_blank\">jsFiddle<\/a>)<\/li>\n<\/ul>\n<h2>Resources<\/h2>\n<ul>\n<li><a href=\"http:\/\/www.developerdrive.com\/2012\/04\/an-introduction-to-underscore-js-%E2%80%93-part-1-arrays\/\" target=\"_blank\">An Introduction to Underscore.js<\/a><\/li>\n<li><a href=\"http:\/\/underscorejs.org\/\" target=\"_blank\">Underscore.js official website<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Underscore.js is a tiny JavaScript utility library that makes working with some of the common data structures used in JavaScript much easier Common Methods _.union(myArray2, myArray3) to concatenate two arrays _.uniq(myArray3) to strip duplicates from array _.zip(myArray, myArray2) to return arrays where the nth value of each array is paired [0, 0], [1, 1], [2, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[144],"tags":[9,88],"class_list":["post-333","post","type-post","status-publish","format-standard","hentry","category-web-shots","tag-javascript","tag-underscore-js"],"_links":{"self":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/comments?post=333"}],"version-history":[{"count":15,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/333\/revisions"}],"predecessor-version":[{"id":3365,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/posts\/333\/revisions\/3365"}],"wp:attachment":[{"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/media?parent=333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/categories?post=333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webninjataylor.com\/library\/wp-json\/wp\/v2\/tags?post=333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}