<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Underscore.js &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/underscore-js/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:44:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Underscore.js &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Underscore.js: Introducing _.chain(&#8230;)</title>
		<link>https://myshittycode.com/2015/07/13/underscore-js-introducing-_-chain/</link>
					<comments>https://myshittycode.com/2015/07/13/underscore-js-introducing-_-chain/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Mon, 13 Jul 2015 15:40:04 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Underscore.js]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=734</guid>

					<description><![CDATA[<p>PROBLEM Let&#8217;s assume we have the following JSON data:- What we want to do is to get all unique employees and ordered them by their names so that we get the following data:- SOLUTION 1: Less Elegant Underscore.js provides various functions that allow us to pull this off. While doable, the code is virtually not [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2015/07/13/underscore-js-introducing-_-chain/">Underscore.js: Introducing _.chain(&#8230;)</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Let&#8217;s assume we have the following JSON data:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
&#x5B;
    {
        &quot;date&quot;: &quot;2015-07-30&quot;,
        &quot;calendarAppointmentPtoJsonBeans&quot;: &#x5B;
            {
                &quot;basicEmployeeJsonBean&quot;: {
                    &quot;id&quot;: 1,
                    &quot;name&quot;: &quot;Vrabel&quot;
            	}
            }
        ]
    },
    {
        &quot;date&quot;: &quot;2015-07-31&quot;,
        &quot;calendarAppointmentPtoJsonBeans&quot;: &#x5B;
            {
                &quot;basicEmployeeJsonBean&quot;: {
                    &quot;id&quot;: 2,
                    &quot;name&quot;: &quot;Cray&quot;
            	}
            },
            {
                &quot;basicEmployeeJsonBean&quot;: {
                    &quot;id&quot;: 1,
                    &quot;name&quot;: &quot;Vrabel&quot;
            	}
            },
            {
                &quot;basicEmployeeJsonBean&quot;: {
                    &quot;id&quot;: 3,
                    &quot;name&quot;: &quot;Haeflinger&quot;
            	}
            }
        ]
    }
]
</pre></div>


<p>What we want to do is to get all unique employees and ordered them by their names so that we get the following data:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
&#x5B;
    {
        &quot;id&quot;: 2,
        &quot;name&quot;: &quot;Cray&quot;
    },
    {
        &quot;id&quot;: 3,
        &quot;name&quot;: &quot;Haeflinger&quot;
    },
    {
        &quot;id&quot;: 1,
        &quot;name&quot;: &quot;Vrabel&quot;
    }
]
</pre></div>


<h2 class="wp-block-heading">SOLUTION 1: Less Elegant</h2>



<p><a href="http://underscorejs.org/" target="_blank" rel="noopener">Underscore.js</a> provides various functions that allow us to pull this off.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
var employees = _.sortBy( _.unique( _.pluck( _.flatten(
                _.pluck( jsonData, &#039;calendarAppointmentPtoJsonBeans&#039; ) ),
                        &#039;basicEmployeeJsonBean&#039; ), function ( employee ) {
                    return employee.id;
                } ), function ( employee ) {
                    return employee.name;
                } );
</pre></div>


<p>While doable, the code is virtually not readable.</p>



<p>If you hate your peers and life, this is what you would write.</p>



<h2 class="wp-block-heading">SOLUTION 2: More Elegant</h2>



<p>The good news is Underscore.js also provides <strong>_.chain(..)</strong> that allows us to do the same thing through method chaining:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
var employees = _.chain( jsonData )
                .pluck( &#039;calendarAppointmentPtoJsonBeans&#039; )
                .flatten()
                .pluck( &#039;basicEmployeeJsonBean&#039; )
                .unique( function ( employee ) {
                    return employee.id;
                } )
                .sortBy( function ( employee ) {
                    return employee.name;
                } )
                .value();
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2015/07/13/underscore-js-introducing-_-chain/">Underscore.js: Introducing _.chain(&#8230;)</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2015/07/13/underscore-js-introducing-_-chain/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">734</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-22 05:12:15 by W3 Total Cache
-->