How to use Lodash compact function?

harithzainudin

Muhammad Harith Zainudin

Posted on May 29, 2022

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)
console.log(cleanWords)
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!

💖 💪 🙅 🚩
harithzainudin
Muhammad Harith Zainudin

Posted on May 29, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related