每天一个lodash方法-compact

compact源码

功能

Creates an array with all falsey values removed. The values false, null, 0, "", undefined, and NaN are falsey.

新建一个移除了所有falsey值的数组。 false, null, 0, "", undefined, and NaN 都是falsey值。(移除所有假值并返回一个新的数组)

使用

compact([0, 1, false, 2, '', 3])
// => [1, 2, 3]

自行实现

function compact(array){
    if(!Array.isArray) return [] //传入的不是数组,直接返回空数组
    
    var result = []
    
    var isFalse = function(){}
    for(var i = 0;i<array.length; i++){
        if(array[i]){
            result.push(array[i])
        }
    }
    
    return result
}

lodash实现方式

function compact(array) {
  let resIndex = 0 // 下标
  const result = [] // 结果集

  if (array == null) { #1
    return result
  }

  for (const value of array) { // 遍历数组,直接取出数组
    if (value) {
      result[resIndex++] = value
    }
  }
  return result
}

array == null

array == null,源码中这里,其实是传入不符合规则的array参数,直接返回一个空数组。如果传入一个数组,会正常进行下去,

等等,我好像读错源码了。

master里的compact有问题。如果我传入一个false,那么当前这个compact会报错。经过仔细查找,在npm-package这个分支里的代码应该是正确的。

function compact(array) {
  var index = -1,
      length = array ? array.length : 0,
      resIndex = 0,
      result = [];

  while (++index < length) {
    var value = array[index];
    if (value) {
      result[resIndex++] = value;
    }
  }
  return result;
}

length = array ? array.length : 0这里。如果传入的是其它类型的值。接下来的都会不符合while loop的判断条件,直接返回一个空数组。

condition 1,传入的是字符串abc,返回一个['a','b','c']
condition 2,传入的是一个function,返回一个[]

引申出来的问题

6个假值

这里有相关的解答

false, null, 0, "", undefined, and NaN.

iffalse){}else{}if函数体内值如果是6个之一都会执行else里的逻辑。

可是

null == false

null == 0

null == '' 

以上无一例外都是false

但是

!null == !false       // returns true

!null == !0           // returns true

!false == !undefined  // returns true
null == undefined     // returns true

false == 0            // returns true

==规则描述

开发中,比起使用==我更倾向于===,因为它更让人模糊不清。

==并不是简单的进行一个boolean的隐士转换再取比较值。而是通过一个比较复杂的递归算法,再检查类型之后,尝试不同类型的数据强制喂转换为相同的类型

下边是我copy过来的规则,x,y为不同的类型情况下的算法逻辑。

  • If x is null and y is undefined, return true.

    // so null == undefined => true

  • If x is undefined and y is null, return true.
    // so und == null true
  • If Type(x) is Number and Type(y) is String,return the result of the comparison x == ToNumber(y).
    // 1 == "1" => 1== Number("1") => true
  • If Type(x) is String and Type(y) is Number,return the result of the comparison ToNumber(x) == y.
    // "2" ==2 => Number("2") == 2 => true
  • If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
    // true == 1 => Number(true) == 1 => 1==1 => true
  • If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
    // 上边左右对掉。
  • If Type(x) is either String or Number and Type(y) is Object,return the result of the comparison x == ToPrimitive(y).
  • If Type(x) is Object and Type(y) is either String or Number,return the result of the comparison ToPrimitive(x) == y.
    //
  • Return false.
    //这就像是switch的default操作。

关于对象,涉及到了ToPrimitive(),需要研究下,暂时不讨论。以上基本解释了 null ==操作为何返回false

关于!操作相关的比较。

使用了!以后相当于做了一个toBoolean转换,类型匹配,这避免了算法的类型强制部分,使其行为基本上像严格相等(===)。
链接描述

为什么undefined == null 为true

这个上边解释咯。规范定义了undefined ==的算法。

至于NaN是个奇葩,它跟自己都不想等。所以==永远都是false
this is answer link

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值