How to check if map or collection is empty?


Zac Delventhal

Using traditional objects in JavaScript, it's easy to check if it's null using :Object.keys

const emptyObj = {};
console.log(Object.keys(emptyObj).length === 0);  // true, i.e. "empty"

const populatedObj = { foo: 'bar' };
console.log(Object.keys(populatedObj).length === 0);  // false, not empty

Although a bit redundant, the same method can be used with arrays if you wish:

const emptyArr = [];
console.log(Object.keys(emptyArr).length === 0);  // true

const populatedArr = [1, 2, 3];
console.log(Object.keys(populatedArr).length === 0);  // false

However, ES6's handy new data structures Map and Set don't work the same way. If you try to use Object.keyson them , you will always get an empty array!

const populatedSet = new Set(['foo']);
console.log(Object.keys(populatedSet).length);  // 0

const populatedMap = new Map([['foo', 1]]);
console.log(Object.keys(populatedMap).length);  // 0

So what's the best way to check if you like using the new ES6 constructs? Also, is there some kind of single overloaded method that works for objects, arrays, maps and collections ?

TJ crowd

you use its sizeproperties. Both Maps and Sets have it ( here and here ).

const populatedSet = new Set(['foo']);
console.log(populatedSet.size);  // 1

const populatedMap = new Map([['foo', 1]]);
console.log(populatedMap.size);  // 1

(Side note: WeakMaps and WeakSets don't have the extra sizefunctionality or some other functionality that their "strong" counterparts have, it would be wise to keep their implementation and code with them. :-) If they have similar functionality, then They provide the same API, but they are not subclassed. )

Related


How to check if map or collection is empty?

Zac Delventhal Using traditional objects in JavaScript, it's easy to check if it's null using :Object.keys const emptyObj = {}; console.log(Object.keys(emptyObj).length === 0); // true, i.e. "empty" const populatedObj = { foo: 'bar' }; console.log(Object.key

How to check if map or collection is empty?

Zac Delventhal Using traditional objects in JavaScript, it's easy to check if it's null using :Object.keys const emptyObj = {}; console.log(Object.keys(emptyObj).length === 0); // true, i.e. "empty" const populatedObj = { foo: 'bar' }; console.log(Object.key

How to check if map or collection is empty?

Zac Delventhal Using traditional objects in JavaScript, it's easy to check if it's null using :Object.keys const emptyObj = {}; console.log(Object.keys(emptyObj).length === 0); // true, i.e. "empty" const populatedObj = { foo: 'bar' }; console.log(Object.key

How to check if map or collection is empty?

Zac Delventhal Using traditional objects in JavaScript, it's easy to check if it's null using :Object.keys const emptyObj = {}; console.log(Object.keys(emptyObj).length === 0); // true, i.e. "empty" const populatedObj = { foo: 'bar' }; console.log(Object.key

A way to check if a Collection or Map is empty or null?

Abaric I want to check if anything in Map, HashMap, ArrayList, List or Collections is empty or empty? I have this, but it doesn't work when I pass the map: protected static <T extends Collection, Map> boolean isCollectionMapNullOrEmpty(final T c) { if (c ==

A way to check if a Collection or Map is empty or null?

Abaric I want to check if anything in Map, HashMap, ArrayList, List or Collections is empty or empty? I have this, but it doesn't work when I pass the map: protected static <T extends Collection, Map> boolean isCollectionMapNullOrEmpty(final T c) { if (c ==

How to check if Firestore collection is empty?

Edric I am using Angular and Angularfire2. I want to check if Firestore collection is empty. I'm making a todo app and I want to display an empty status container as part of a material guide . Here is my data structure: users/ userID/ todos/ // etc

How to check if map is empty in Golang?

030: When the following code: if map == nil { log.Fatal("map is empty") } When running, the log statement is not executed, but the fmt.Println(map)representation map is empty: map[] Jacob: You can use len: if len(map) == 0 { .... } from https://gola

How to check if map is empty in Golang?

030: When the following code: if map == nil { log.Fatal("map is empty") } When running, the log statement is not executed, but the fmt.Println(map)representation map is empty: map[] Jacob: You can use len: if len(map) == 0 { .... } from https://gola

How to check if map is empty in Golang?

030: When the following code: if map == nil { log.Fatal("map is empty") } When running, the log statement is not executed, but the fmt.Println(map)representation map is empty: map[] Jacob: You can use len: if len(map) == 0 { .... } from https://gola

How to check if map is empty in Golang?

030: When the following code: if map == nil { log.Fatal("map is empty") } When running, the log statement is not executed, but the fmt.Println(map)representation map is empty: map[] Jacob: You can use len: if len(map) == 0 { .... } from https://gola

How to check if map is empty in Golang?

030 When the following code: if map == nil { log.Fatal("map is empty") } When running, the log statement is not executed, but the fmt.Println(map)representation map is empty: map[] Jacob You can use len: if len(map) == 0 { .... } from https://golang

How to check if map is empty in Golang?

030: When the following code: if map == nil { log.Fatal("map is empty") } When running, the log statement is not executed, but the fmt.Println(map)representation map is empty: map[] Jacob: You can use len: if len(map) == 0 { .... } from https://gola

How to check if map is empty in Golang?

030: When the following code: if map == nil { log.Fatal("map is empty") } When running, the log statement is not executed, but the fmt.Println(map)representation map is empty: map[] Jacob: You can use len: if len(map) == 0 { .... } from https://gola

How to check if Collection is empty using Java Stream

Saurabh Kumar I am new to Java 8 and I am not able to understand the error in the following code. The idea is to send Collection<User>if it's not null. But if the collection is empty than send the HttpStatus.NOT_FOUNDentity response. @RequestMapping(value = "/

How to check if a collection in view blade Laravel is empty?

successful man If I'm dd($items);in the controller, the result is as follows: In view blade laravel I check if the collection is empty: @if($items) ... @endif but this doesn't work How can I solve this problem? my library You can use $items->isEmpty(); or $it

How to check if Collection is empty using Java Stream

Saurabh Kumar I am new to Java 8 and I am not able to understand the error in the following code. The idea is to send Collection<User>if it's not null. But if the collection is empty than send the HttpStatus.NOT_FOUNDentity response. @RequestMapping(value = "/

LogicApp - How to check if a collection is empty using length

Greg In my JSON result I am trying to see if a particular object exists and if so what is the count. I am using lengthan expression but it seems to fail for me with the error: The template function 'lenth' is not defined or not valid. Here is my Get_Ticketobj

How to check f# if collection is empty?

cute How can i check if the collection is empty, if the collection has an empty collection, i have implemented the following code, then i should get the value true else false, eg: [1; 2; []] the result of this collection should be true and This should result i

How to check if Collection is empty using Java Stream

Saurabh Kumar I am new to Java 8 and I am not able to understand the error in the following code. The idea is to send Collection<User>if it's not null. But if the collection is empty than send the HttpStatus.NOT_FOUNDentity response. @RequestMapping(value = "/

How to check if a collection in view blade Laravel is empty?

successful man If I'm dd($items);in the controller, the result is as follows: In view blade laravel I check if the collection is empty: @if($items) ... @endif but this doesn't work How can I solve this problem? my library You can use $items->isEmpty(); or $it

LogicApp - How to check if a collection is empty using length

Greg In my JSON result I am trying to see if a particular object exists and if so what is the count. I am using lengthan expression but it seems to fail for me with the error: The template function 'lenth' is not defined or not valid. Here is my Get_Ticketobj

How to check if Collection is empty using Java Stream

Saurabh Kumar I am new to Java 8 and I am not able to understand the error in the following code. The idea is to send Collection<User>if it's not null. But if the collection is empty than send the HttpStatus.NOT_FOUNDentity response. @RequestMapping(value = "/

How to check if Collection is empty using Java Stream

Saurabh Kumar I am new to Java 8 and I am not able to understand the error in the following code. The idea is to send Collection<User>if it's not null. But if the collection is empty than send the HttpStatus.NOT_FOUNDentity response. @RequestMapping(value = "/

LogicApp - How to check if a collection is empty using length

Greg In my JSON result I am trying to see if a particular object exists and if so what is the count. I am using lengthan expression but it seems to fail for me with the error: The template function 'lenth' is not defined or not valid. Here is my Get_Ticketobj

How to check if a collection in view blade Laravel is empty?

successful man If I'm dd($items);in the controller, the result is as follows: In view blade laravel I check if the collection is empty: @if($items) ... @endif but this doesn't work How can I solve this problem? my library You can use $items->isEmpty(); or $it

LogicApp - How to check if a collection is empty using length

Greg In my JSON result I am trying to see if a particular object exists and if so what is the count. I am using lengthan expression but it seems to fail for me with the error: The template function 'lenth' is not defined or not valid. Here is my Get_Ticketobj

LogicApp - How to check if a collection is empty using length

Greg In my JSON result I am trying to see if a particular object exists and if so what is the count. I am using lengthan expression but it seems to fail for me with the error: The template function 'lenth' is not defined or not valid. Here is my Get_Ticketobj

LogicApp - How to check if a collection is empty using length

Greg In my JSON result I am trying to see if a particular object exists and if so what is the count. I am using lengthan expression but it seems to fail for me with the error: The template function 'lenth' is not defined or not valid. Here is my Get_Ticketobj