javascript - strange behavior when using Underscore when testing equals condition -
i using underscore.js in project , came across weird behavior. when filter array changes_summary === 2
getting desired result when use same changes_summay === 0
, not getting output.
following code:
var result={ "data": [ { "id": 956, "changes_summary": 2, "name": "pradeep", "age": 32 }, { "id": 956, "changes_summary": 0, "name": "praveen", "age": 22 } ] } var filtered = _.filter(result.data, function(obj){ return (obj.changes_summary && obj.changes_summary === 2) }); //working when comparing changes_summary 2 not working when comparing 0 console.log(filtered);
please let me know going wrong. link jsfiddle attached: jsfiddle
the first test — obj.changes_summary
— fail when property value 0
because that's falsy value. if you're testing see if property present in object, can this:
return (("changes_summary" in obj) && obj.changes_summary === 0);
Comments
Post a Comment