[removed] filter() for Objects

前端 未结 16 970
心在旅途
心在旅途 2020-11-22 15:23

ECMAScript 5 has the filter() prototype for Array types, but not Object types, if I understand correctly.

How would I implemen

16条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 15:42

    If you're willing to use underscore or lodash, you can use pick (or its opposite, omit).

    Examples from underscore's docs:

    _.pick({name: 'moe', age: 50, userid: 'moe1'}, 'name', 'age');
    // {name: 'moe', age: 50}
    

    Or with a callback (for lodash, use pickBy):

    _.pick({name: 'moe', age: 50, userid: 'moe1'}, function(value, key, object) {
      return _.isNumber(value);
    });
    // {age: 50}
    

提交回复
热议问题