Get object keys for filtered values

前端 未结 14 1395
傲寒
傲寒 2021-02-05 03:31

The case is simple - I\'ve got a following object:

Object {1: false, 2: true, 3: false, 4: false, 5: false, 6: false, 7: false, 8: true, 12: false, 13: false, 14         


        
14条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 04:03

    var keys = [];
    _.each( obj, function( val, key ) {
      if ( val ) {
        keys.push(key);
      }
    });
    

    There may be easier/shorter ways using plain Underscore.


    In case anyone here uses Lodash instead of Underscore, the following is also possible, which is very short and easy to read:

    var keys = _.invert(obj, true)[ "true" ];
    

提交回复
热议问题