DEV Community

Muhammad Harith Zainudin
Muhammad Harith Zainudin

Posted on • Edited on

3 3

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!

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more