Basics of Method Chaining in JavaScript using Lodash

Dataflow Diagram for method chaining

y = f4(f3(f2(f1(x))

x1 = f1(x);

x2 = f2(x1);

x3 = f3(x2);

y = f4(x3);

var users = [
{
"user": "barney",
"age": 36
},
{
"user": "fred",
"age": 40
},
{
"user": "pebbles",
"age": 1
}
]
// youngest = "pebbles is 1"
var youngest = _.head(_.map(_.sortBy(users, 'age'), o=>o.user + ' is ' + o.age));
// youngest = "pebbles is 1"
var youngest = _.chain(users).sortBy('age').map(o=>o.user +' is ' + o.age).head().value();
var chainedTransformer = _.chain(users).sortBy('age').map(o=>o.user + ' is ' + o.age);// youngest = "pebbles is 1"
var youngest = chainedTransformer.head().value();
// oldest = "fred is 40"
var oldest = chainedTransformer.reverse().head().value();
// youngest = "pebbles is 1"
var youngest = _.head(_.map(_.sortBy(users, 'age'), o=>o.user + ' is ' + o.age));
// oldest = "fred is 40"
var oldest = _.head(_.reverse(_.map(_.sortBy(users, 'age'), o=>o.user + ' is ' + o.age)));
_.map(collection, (value, idx/key, collection)=> {
// perform logic
});
Peace out!!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

PlatON Bi-weekly Report 0215 | PlatON Under Internal Testing and Recruits Global Developer…

The Merit of Doing Things the Hard Way

Java Program to Check If a Number is Neon Number or Not

My Journey As A Self-Taught Programmer

UML Sequence Diagram Through “The Little Prince” Narrator’s Interactions With the Outside World

Covid-19 API on Android Studio

Python Harmonic Analysis Curving fitting for the Exported Time Series Data From Google Earth Engine

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Parshwa Mehta

Parshwa Mehta

More from Medium

TypeScript Vs JavaScript: What is the Difference?

How to Get Unique Values from a List in JavaScript

OBJECT ORIENTED PROGRAMMING(OOPs) USING TYPESCRIPT

I have never had the privilege to use dynamic imports until very recently when I used them to power…