"You Don't Need Lodash" is intened to show how modern javascript can replace most of the Lodash library.
9+ years after Lodash's initial release, the browser landscape has drastically changed. The purpose of this is not to tell you that you shouldn't use Lodash, but rather to re-educate you on what exactly Lodash is useful for. The JavaScript standard library, and other browser APIs, have been much better standardized, and many of the previous pitfalls of compatibility no longer exist. While Lodash is still useful, it is less so than before, and it's important for you--the developer--to be familiar with the underlying APIs that libraries are abstracting. A lot of the new APIs and methodologies are much easier to understand, and are sometimes more coherent than those in libraries like Lodash.
Most of the APIs that I'll be showing can be polyfilled, meaning that if the browser is modern, and supports the APIs, it will use those, but if the browser is legacy, it will update the APIs with the new features, and allow all browsers to work. Modern features that can be polyfilled for legacy browsers:
- Promise
- fetch
- classList
- Array.from
- Object.assign
- More... Although a couple of the modern examples have more characters in their code, they should not deter you from trying to understand these new APIs. Read carefully, and try to understand what the code is doing so that you can better reflect on whether or not you should use a library.
Lodash
_.compact([0, 1, false, 2, "", 3]);
Modern | Using Array.filter and Arrow functions
[0, 1, false, 2, "", 3].filter(element => element);
Lodash
const array = [1];
const other = _.concat(array, 2, [3], [[4]]);
Modern | Using Array.concat
const array = [1];
const other = array.concat(2, [3], [[4]]);
Lodash
_.drop([1, 2, 3], 2);
Modern | Using Array.slice
[1, 2, 3].slice(2);
Lodash
_.fill([4, 6, 8, 10], "*", 1, 3);
Modern | Using Array.fill
[4, 6, 8, 10].fill("*", 1, 3);
Lodash
const users = [
{ user: "barney", active: false },
{ user: "fred", active: false },
{ user: "pebbles", active: true }
];
_.findIndex(users, o => o.user == "barney");
Modern | Using Array.findIndex
const users = [
{ user: "barney", active: false },
{ user: "fred", active: false },
{ user: "pebbles", active: true }
];
users.findIndex(o => o.user == "barney");
Lodash
_.head([1, 2, 3]);
Modern
[1, 2, 3][0];
Lodash
_.flatten([1, [2, [3, [4]], 5]]);
New | Using Array.flat
[1, [2, [3, [4]], 5]].flat(1);
Lodash
_.flattenDeep([1, [2, [3, [4]], 5]]);
New | Using Array.flat
[1, [2, [3, [4]], 5]].flat(Infinity);
Lodash
const array = [1, [2, [3, [4]], 5]];
_.flattenDepth(array, 1);
New | Using Array.flat
const array = [1, [2, [3, [4]], 5]];
array.flat(1));
Lodash
_.fromPairs([["a", 1], ["b", 2]]);
New | Using Object.fromEntries
Object.fromEntries([["a", 1], ["b", 2]]);
Lodash
_.indexOf([1, 2, 1, 2], 2, 2);
Modern | Using Array.indexOf
[1, 2, 1, 2].indexOf(2, 2);
Lodash
_.initial([1, 2, 3]);
Modern | Using Array.slice
[1, 2, 3].slice(0, -1);
Lodash
_.join(["a", "b", "c"], "~");
Modern | Using Array.join
["a", "b", "c"].join("~");
Lodash
_.last([1, 2, 3]);
Modern | Using Array.slice
[1, 2, 3].slice(-1)[0];
Lodash
const array = [1, 2, 3];
_.reverse(array);
Modern | Using Array.reverse
const array = [1, 2, 3];
array.reverse();
Lodash
_.tail([1, 2, 3]);
Modern | Using Array.slice
[1, 2, 3].slice(1);
Lodash
_.take([1, 2, 3], 2);
Modern | Using Array.slice
[1, 2, 3].slice(0, 2);
Lodash
_.takeRight([1, 2, 3], 2);
Modern | Using Array.slice
[1, 2, 3].slice(-2);