Get object keys for filtered values

前端 未结 14 1396
傲寒
傲寒 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:05

    Heres a way to do it without using any libraries in ES6

    let z = {2: true, 3:false, 8:true, 9:false, 10: false}
    Array.from(new Set(Object.keys(z).map(function(k){if(z[k]){return k}}))).filter(f=>f)
    

    and out you get [2, 8]

    0 讨论(0)
  • 2021-02-05 04:07
    function findKey(obj, value){
        var key;
    
        _.each(_.keys(obj), function(k){
          var v = obj[k];
          if (v === value){
            key = k;
          }
        });
    
        return key;
    }
    
    0 讨论(0)
提交回复
热议问题