DEV Community

Muhammad Harith Zainudin
Muhammad Harith Zainudin

Posted on • Updated on

How to use Lodash compact function?

Note: This tutorial also exist in my GitHub

Compact function will remove all falsey values in the array that you provided

Falsey values in javascript are something that evaluates to FALSE.
There are only six falsey values in JavaScript: undefined, null, NaN, 0, "" (empty string), and false.

How to use it? Simple.

const falseyValues = ["", null, undefined, 0, "Harith", "Malaysia"];

const cleanWords = _.compact(data.randomWord)
Enter fullscreen mode Exit fullscreen mode

The result will be
Result of lodash compact function

It will remove all the falsey values in the array that we provided. Simple as that

Thank you for reading :D

Psstt pstt :p
Do consider to love this article ❤️ and follow me! Why not right? It's FREE~
I would really appreciate it 👨🏻‍💻
Will be posting more on things related to AWS, Javascript, Python, Serverless and more!

Top comments (0)