{"id":1854,"date":"2018-04-17T22:06:28","date_gmt":"2018-04-18T03:06:28","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1854"},"modified":"2018-04-17T22:06:28","modified_gmt":"2018-04-18T03:06:28","slug":"javascript-how-to-find-the-count-of-an-element-in-an-array-using-filter","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/17\/javascript-how-to-find-the-count-of-an-element-in-an-array-using-filter\/","title":{"rendered":"JavaScript: How to find the count of an element in an array using filter( )"},"content":{"rendered":"<p>Let&#8217;s say we have an array of numbers in JavaScript that looks like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let array = [1, 2, 3, 5, 2, 8, 9, 2];<\/pre>\n<p>and we want to count the number of 2&#8217;s in the array.\u00a0 A quick and direct way to get the count of 2&#8217;s would be to do something like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">console.log(array.filter(x =&gt; x === 2).length);<\/pre>\n<p>which yields 3. A better way to do this is probably to set up a function that will return the count. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">function returnCount(arrayToCheck, element){\r\n  let array = arrayToCheck;\r\n  let elementToFind = element;\r\n  let numberFound = array.filter(x =&gt; x === elementToFind).length;\r\n  return numberFound;\r\n}<\/pre>\n<p>Then call this function to get the result:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">console.log(returnCount(array, 2));<\/pre>\n<p>which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">3<\/pre>\n<p>Here is what w3schools.com says about the JavaScript filter method:<\/p>\n<blockquote><p>The filter() method creates an array filled with all array elements that pass a test (provided as a function).<\/p>\n<p>filter() does not execute the function for array elements without values.<\/p>\n<p>filter() does not change the original array.<\/p><\/blockquote>\n<p>For more information about the JavaScript filter() method, see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/jsref\/jsref_filter.asp\">https:\/\/www.w3schools.com\/jsref\/jsref_filter.asp<\/a><br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/filter\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/filter<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say we have an array of numbers in JavaScript that looks like this: let array = [1, 2, 3, 5, 2, 8, 9, 2]; and we want to count the number of 2&#8217;s in the array.\u00a0 A quick and direct way to get the count of 2&#8217;s would be to do something like this: &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/17\/javascript-how-to-find-the-count-of-an-element-in-an-array-using-filter\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript: How to find the count of an element in an array using filter( )<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[127,45],"class_list":["post-1854","post","type-post","status-publish","format-standard","hentry","category-javascript-language","tag-filter","tag-javascript"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1854"}],"version-history":[{"count":4,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1854\/revisions"}],"predecessor-version":[{"id":1858,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1854\/revisions\/1858"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}