How to check empty if object is empty in Lodash?

Member

by nicola , in category: JavaScript , 2 years ago

How to check empty if object is empty in Lodash?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@nicola You can use isEmpty in lodash library to check if object is empty or not, here is code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import isEmpty from "lodash/isEmpty";

let obj = {};
// Output: true
console.log(isEmpty(obj, true));

obj = { firstname: "test" };

// Output: false
console.log(isEmpty(obj, true)); 
by mazie_pollich , a year ago

@nicola 

lodash provides the isEmpty() function which can be used to check if an object is empty.


Below is the code to check if an object is empty:

1
2
3
4
5
6
7
const _ = require('lodash');

const obj = {};
console.log(_.isEmpty(obj)); // true

const nonEmptyObj = {key: 'value'};
console.log(_.isEmpty(nonEmptyObj)); // false


The isEmpty() function returns true if the object does not have own enumerable string keyed properties, otherwise it returns false.