In filter
function of lodash if the callback (you passed as argument) return truthy value that element considered to be retained in resultant array otherwise (in case return falsy) it will not retain that element. Your isEmpty
return true
if it is Empty and thus the result retains those values (null
, undefined
, 0
, ...). So you can either use
_.filter(myArray, _.negate(_.IsEmpty))
or _.filter(myArray, v => !_.IsEmpty(v))
In the way you are trying
Or, you can directly use _.filter(myArray)
but in this case it will not remove empty object or empty array as same like _.filter(myArray, Boolean)
, passing Boolean
is not necessery in case of using lodash
in case you don't want to negate and want a simpler solution for removing all the empty element, then you can use
_.reject(myArray, _.isEmpty)