Use Underscore.js to remove object from array based on property

javascript, underscore.js

Solution

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; });

Problem

I have an array of objects in javascript. Each object is of the form ``` obj { location: "left", // some string weight: 0 // can be zero or non zero } ``` I want to return a filtered copy of the array where the objects with a weight property of zero are removed What is the clean way to do this with underscore?

Original source