Use Underscore.js to remove object from array based on property
You don't even really need underscore for this, since there's the filter
method as of ECMAScript 5:
var newArr = oldArr.filter(function(o) { return o.weight !== 0; });
But if you want to use underscore (e.g. to support older browsers that do not support ECMAScript 5), you can use its filter
method:
var newArr = _.filter(oldArr, function(o) { return o.weight !== 0; });
filter
should do the job
_.filter(data, function(item) { return !!item.weight; });
the !!
is used to cast the item.weight
into a boolean value, where NULL
, false
or 0
will make it false
, and filter it out.
This should do it:
_.filter(myArray, function(o){ return o.weight; });