天天一个lodash方法-compact

compact源码javascript

功能

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

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

使用

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参数,直接返回一个空数组。若是传入一个数组,会正常进行下去,github

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

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

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个假值

这里有相关的解答oop

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

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

相关文章
相关标签/搜索