Filtering object properties based on value

前端 未结 7 758
孤独总比滥情好
孤独总比滥情好 2020-12-14 16:40

Is there some elegant way of filtering out falsey properties from this object with lodash/underscore? Similar to how _.compact(array) removes falsey elements fr

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-14 17:09

    Unfortunately I cannot direclty comment on the posts above yet, so I create this extra post.

    Since Lodash v4 the functionality described above has been moved to _.pickBy. With _.identity as default you could also change your code to:

    var filtered = _.pickBy(obj);
    

    See this JSBin for a working example.

提交回复
热议问题