【问题标题】:select all object keys that start with an underscore( _ )选择所有以下划线(_)开头的对象键
【发布时间】:2019-05-16 04:41:10
【问题描述】:

我需要在以下对象中创建一个包含所有键(不是值)的数组,其中键以 _ 下划线开头...

在下面的 sn-p 中,我试图让 getSubscriptions() 返回 ["_foo1", "_foo2"]

let myObj = {
  foo0: 'test',
  _foo1: 'test',
  _foo2: 'test',
  foo3: 'test',
};

function getSubscriptions(obj, cb) {
    // should return ["_foo1", "_foo2"]
    let ret = ["foo1", "_foo2"];
    return cb(ret);
}
getSubscriptions(myObj, (ret) => {
    if (match(ret, ["_foo1", "_foo2"]) ) { 
        $('.nope').hide();
        return $('.works').show(); 
    }
    $('.nope').show();
    $('.works').hide();
});

function match(arr1, arr2) {
    if(arr1.length !== arr2.length) { return false; } 
    for(var i = arr1.length; i--;) { 
        if(arr1[i] !== arr2[i]) { return false;}  
    }
    return true; 
}
body {
    background: #333;
    color: #4ac388;
    padding: 2em;
    font-family: monospace;
    font-size: 19px;
}
.nope {
  color: #ce4242;
}
.container div{
  padding: 1em;
  border: 3px dotted;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
    <div class="works">It Works!</div>
    <div class="nope">
    Doesn't Work...</div>
</div>

【问题讨论】:

    标签: javascript arrays object ecmascript-6


    【解决方案1】:

    您可以使用Object.keysArray.filter

    let myObj = {
      foo0: 'test',
      _foo1: 'test',
      _foo2: 'test',
      foo3: 'test',
    };
    
    let result = Object.keys(myObj).filter(v => v.startsWith("_"));
    console.log(result);

    【讨论】:

      【解决方案2】:

      使用Object.keysfilter,并检查第一个字符是否为下划线_

      let myObj = {
        foo0: 'test',
        _foo1: 'test',
        _foo2: 'test',
        foo3: 'test',
      };
      
      const res = Object.keys(myObj).filter(([c]) => c == "_");
      console.log(res);

      【讨论】:

        【解决方案3】:

        在过滤器中尝试.startsWith("_")

        let myObj = {
          foo0: 'test',
          _foo1: 'test',
          _foo2: 'test',
          foo3: 'test',
        };
        
        function getSubscriptions(obj, cb) {
            // should return ["_foo1", "_foo2"]
            let ret = Object.keys(myObj).filter(v => v.startsWith("_"));
        
            return cb(ret);
        }
        getSubscriptions(myObj, (ret) => {
            if (match(ret, ["_foo1", "_foo2"]) ) { 
                $('.nope').hide();
                $('.works').show(); 
            }
           // $('.nope').show();  // you have misplaced these 2 lines
            //$('.works').hide();
        });
        
        function match(arr1, arr2) {
            if(arr1.length !== arr2.length) { return false; } 
            for(var i = arr1.length; i--;) { 
                if(arr1[i] !== arr2[i]) { return false;}  
            }
            return true; 
        }
        body {
            background: #333;
            color: #4ac388;
            padding: 2em;
            font-family: monospace;
            font-size: 19px;
        }
        .nope {
          color: #ce4242;
        }
        .container div{
          padding: 1em;
          border: 3px dotted;
        }
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <div class="container">
            <div class="works">It Works!</div>
            <div class="nope">
            Doesn't Work...</div>
        </div>

        【讨论】:

          【解决方案4】:

          如下替换你的getSubscriptions()

           function getSubscriptions(obj, cb) {
                  let ret = Object.keys(myObj).filter(ele => ele.startsWith('_'))
                  return cb(ret)
              }
          
          • Object.keys(yourObject) : 返回你的对象的键。

          • Array.filter(function):根据
            从数组中返回过滤值 真实情况

          • String.startsWith : 如果传递的字符串以开头,则返回 true 或 false ('_')

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-10-06
            • 1970-01-01
            • 2014-03-16
            • 1970-01-01
            • 2016-04-06
            相关资源
            最近更新 更多