Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter null values from a Map in JavaScript

Is it possible to filter null values from a map?

const myMap = new Map<string, string|undefined>([
  ['id1', 'value1'],
  ['id2', null],
  ['id3', 'value3'],
  ['id4', null],
]);

I would like my map with id1 and id4 only because the other ids have null values.

Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can convert to array, filter and create a map again:

const myMap = new Map([
  ['id1', 'value1'],
  ['id2', null],
  ['id3', 'value3'],
  ['id4', null],
]);
const res = new Map(Array.from(myMap).filter(val => val[1]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading