How to remove empty strings from Array in Javascript

Posted on October 9, 2021

Here the snippet:

var arr = ['a', '', 'b', 'other', null]

var noEmptyString = arr.filter(d => d && d !== '')

// ['a', 'b', 'other']

In case you have an array of objects:

var arr = [
  {key: 'a'},
  {key: ''},
  {key: 'b'},
  {key: 'other'},
  {},
  {key: null}
]

var noEmptyString = arr.filter(d => d.key && d.key !== '')

// [{key: 'a'}, {key: 'b'}, {key: 'other'}]