{"id":3596,"date":"2016-04-07T12:13:52","date_gmt":"2016-04-07T12:13:52","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3596"},"modified":"2016-04-07T12:13:52","modified_gmt":"2016-04-07T12:13:52","slug":"get-every-value-list-lodash","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/get-every-value-list-lodash\/","title":{"rendered":"Get every other value from a list in lodash"},"content":{"rendered":"<p>You can filter a list in lodash to get every other value. This is useful if, e.g. you are using solr, which returns facet results as a list of alternating facet name and the count (i.e. [&#8220;DropBox&#8221;, 100, &#8220;GoogleDocs&#8221;, 10, &#8220;OneDrive&#8221;, 0])<\/p>\n<p>If you want the odd values:<\/p>\n<pre lang=\"javascript\">\n_.filter(['a','b','c','d'], \n  (value, idx) => idx % 2 === 0\n);\n\n['a', 'c']\n<\/pre>\n<p>If you want the even values:<\/p>\n<pre lang=\"javascript\">\n_.filter(['a','b','c','d'], \n  (value, idx) => idx % 2 === 1\n);\n\n['a', 'c']\n<\/pre>\n<p>As an alternate option, you can turn the list into lists of pairs, like so (you can turn this into a map with &#8220;fromPairs&#8221;).<\/p>\n<p>Starting data:<\/p>\n<pre>\n[\"DropBox\", 0, \"GoogleDocs\", 0, \"OneDrive\", 0]\n<\/pre>\n<p>Result:<\/p>\n<pre lang=\"javascript\">\n[[\"Dropbox, 0], [\"GoogleDocs\", 0], [\"OneDrive\", 0]]\n<\/pre>\n<pre lang=\"javascript\">\n _.values(\n  _.groupBy(\n    _.toPairs(values), \n      (v) => v[0] - v[0] % 2\n    )\n    .map( \n      (v) => [v[0][1], v[1][1]] \n);\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can filter a list in lodash to get every other value. This is useful if, e.g. you are using solr, which returns facet results as a list of alternating facet name and the count (i.e. [&#8220;DropBox&#8221;, 100, &#8220;GoogleDocs&#8221;, 10, &#8220;OneDrive&#8221;, 0]) If you want the odd values: _.filter([&#8216;a&#8217;,&#8217;b&#8217;,&#8217;c&#8217;,&#8217;d&#8217;], (value, idx) => idx % 2 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/get-every-value-list-lodash\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Get every other value from a list in lodash&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,343,517],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3596"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3596"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3596\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}