Shawn Shawn - 4 months ago 19
AngularJS Question

Angularjs - falsy value filter

I have an array of objects like this:

var array = [{title:''},{title:'obj1'},{title:null,desc:'obj2'}];


I want to get only objects where
title
is not a falsy value (exists, not
null
, not empty string, etc).

So far I have this:

filterFilter(array,{title:'!'},true);


But I still get objects where
title
is
null
. How do I do this?

Answer

Using Array.prototype.filter()

var filtered = array.filter(function(item){
    return item.title;
})