Read the Lodash source code -- compact.js

Keywords: Javascript github

There is a general sky Gang number, which changes in thirty-six ways; there is a general evil number, which changes in seventy-two ways -- "journey to the west"

Compact.js

Lodash second api_.compact(array)
It means to delete all the values in the array that can be converted to false through Boolean values, such as null, false, '', 0, undefined, NaN, and return a new array

for instance:

_.compact([0, 1, false, 2, '', 3]);

// => [1, 2, 3]

First look at the source code of compact.js

/**
 * Creates an array with all falsey values removed. The values `false`, `null`,
 * `0`, `""`, `undefined`, and `NaN` are falsey.
 *
 * @since 0.1.0
 * @category Array
 * @param {Array} array The array to compact.
 * @returns {Array} Returns the new array of filtered values.
 * @example
 *
 * compact([0, 1, false, 2, '', 3])
 * // => [1, 2, 3]
 */
function compact(array) {
  let resIndex = 0
  const result = []

  if (array == null) {
    return result
  }

  for (const value of array) {
    if (value) {
      result[resIndex++] = value
    }
  }
  return result
}

export default compact

Analyze the source code

  1. First, create a function compact to pass a parameter

  2. Initializes a variable resIndex to 0 for recording array indexes

  3. Create an empty array result and save the array values after filtering

  4. First, judge whether the parameter is empty. If it is empty, this returns an array

  5. Use the for of loop to remove null, false, 0, undefined, NaN and ''

Reflection

The compact function uses the for of method in es6, so it can also be implemented by the for loop method

for loop first edition

function compact (array) {
  const result = []
  if (array === null) {
    return result
  }
  for (let i = 0; i < array.length; i++) {
    let value = array[i]
    if (arr[i]) {
      result.push(value)
    }
  }
  return result
}

for loop version 2

function compact (array) {
  let resIndex = 0
  const result = []
  if (array === null) {
    return result
  }
  for (let i = 0; i < array.length; i++) {
    const value = array[i]
    if (value) {
      result[resIndex++] = value
    }
  }
  return result
}

The difference between the first version and the second version is in the way of adding arrays. The first version uses push, while the second version uses index assignment. The second version will be faster in terms of speed

while Loop

function compact (array) {
  const result = [] 
  if (array === null) {
    return result
  }
  let index = 0 
  let resIndex = 0 
  while (index < arr.length) {
    let value = array[index]
    if (value) {
      result[resIndex++] = value
    }
    index++
  }
  return result
}

map method

function compact (array) {
  const result = []
  if (array === null) {
    return result
  }
  array.map(function (value) {
    if(value){
      result.push(value)
    }
  })
  return result
}

for in loop

function compact (array) {
  const result = []
  if (array === null) {
    return result
  }
  let resIndex = 0
  for (let index in array) {
    const value = array[index]
    if (value) {
      result[resIndex++] = value
    }
  }
  return result
}

exception

Generally, we don't need to remove the 0 in the array in the development process. We can achieve this function by slightly changing the source code

function compact (array) {
  let resIndex = 0 
  const result = [] 
  if (arr === null) {
    return result
  }
  for (const value of array) {
    if (value || value === 0) {
      result[resIndex++] = value
    }
  }
  return result
}

reference material

  1. Lodash source code explanation (3)-compact function
  2. https://github.com/Hushabyme/lodash-comments/blob/master/compact.js
  3. https://github.com/yeyuqiudeng/pocket-lodash/blob/master/compact.md

Posted by BoukeBuffel on Wed, 22 Apr 2020 08:34:51 -0700