numpy数组与布尔数组_数组和布尔

numpy数组与布尔数组

One of the annoyances of old-school JavaScript was side effects; then Array.prototype got methods like filter, map, and forEach so we didn't need to burn variables before looping over values.  I can't explain how happy I am that the JavaScript language continues to evolve.

老式JavaScript的烦恼之一是副作用。 然后Array.prototype获得了filtermapforEach类的方法,因此我们不需要在遍历值之前刻录变量。 我无法解释JavaScript语言的不断发展让我多么高兴。

Every once in a while I need to filter an array by not its original value but instead a new value, so I use map:

每过一段时间,我需要进行过滤而不是它的原始值的阵列,而是一个新的价值,所以我使用map


myArray.map(item => {
    // Do whatever processing...

    // If we don't care about the item, return false or null or undefined
    return false;
});


While I get the new values I want, sometimes if an iteration returns a result I don't want, I return null or false, which is great, but then I'm left with a bunch of useless items in the resulting array.  The next step is using filter, in which case I could do:

当我获得所需的新值时,有时如果迭代返回了我不想要的结果,我将返回null或false,这很好,但是随后在结果数组中留下了一堆无用的项。 下一步是使用过滤器,在这种情况下,我可以这样做:


myArray
    .map(item => {
        // ...
    })
    // Get rid of bad values
    .filter(item => item);


Since the values I don't want aren't truthy, the filter above removes those bad items.  Did you know there's a clearer way with Boolean?

由于我不想要的值不真实,因此上面的过滤器删除了这些不良项。 您是否知道Boolean有更清晰的方法?


myArray
    .map(item => {
        // ...
    })
    // Get rid of bad values
    .filter(Boolean);


If the value isn't truthy the item is filtered out and I'm left with only the items I want!

如果值不正确,该项目将被过滤掉,而我只剩下我想要的项目!

翻译自: https://davidwalsh.name/array-boolean

numpy数组与布尔数组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值