_.pull(array, [values])
28
_.pull(array, [values])
_.pull方法从数组里删除所有给定值,使用SameValueZero规则判断值是否相等
参数
array (Array): 要修改的数组
[values] (...*): 要被删除的元素组成的数组
返回值
(Array): 返回删除元素后的原数组
例子
var array = ['a', 'b', 'c', 'a', 'b', 'c']; _.pull(array, 'a', 'c'); console.log(array); // => ['b', 'b']
源代码:
import pullAll from './pullAll.js' /** * Removes all given values from `array` using * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero) * for equality comparisons. * * **Note:** Unlike `without`, this method mutates `array`. Use `remove` * to remove elements from an array by predicate. * * @since 2.0.0 * @category Array * @param {Array} array The array to modify. * @param {...*} [values] The values to remove. * @returns {Array} Returns `array`. * @see pullAll, pullAllBy, pullAllWith, pullAt, remove, reject * @example * * const array = ['a', 'b', 'c', 'a', 'b', 'c'] * * pull(array, 'a', 'c') * console.log(array) * // => ['b', 'b'] */ //从数组里删除所有给定值,使用SameValueZero规则判断值是否相等 function pull(array, ...values) {//array是需要删除元素的数组,values是需要删除的元素 return pullAll(array, values)//调用pullAll } export default pull
pullAll
import basePullAll from './.internal/basePullAll.js' /** * This method is like `pull` except that it accepts an array of values to remove. * * **Note:** Unlike `difference`, this method mutates `array`. * * @since 4.0.0 * @category Array * @param {Array} array The array to modify. * @param {Array} values The values to remove. * @returns {Array} Returns `array`. * @see pull, pullAllBy, pullAllWith, pullAt, remove, reject * @example * * const array = ['a', 'b', 'c', 'a', 'b', 'c'] * * pullAll(array, ['a', 'c']) * console.log(array) * // => ['b', 'b'] */ //和pull类似,删除数组里的给定元素,和difference不同,这个方法会改变原数组 function pullAll(array, values) { return (array != null && array.length && values != null && values.length) ? basePullAll(array, values) : array //如果array不是null,并且array有元素,并且values不为null,并且values有元素,就调用basePullAll //否则返回原数组 } export default pullAll
basePullAll
import map from '../map.js' import baseIndexOf from './baseIndexOf.js' import baseIndexOfWith from './baseIndexOfWith.js' import copyArray from './copyArray.js' /** * The base implementation of `pullAllBy`. * * @private * @param {Array} array The array to modify. * @param {Array} values The values to remove. * @param {Function} [iteratee] The iteratee invoked per element. * @param {Function} [comparator] The comparator invoked per element. * @returns {Array} Returns `array`. */ //pullAllBy方法的基础实现 //array要修改的数组,values要删除的元素,iteratee迭代器,comparator比较器 function basePullAll(array, values, iteratee, comparator) { const indexOf = comparator ? baseIndexOfWith : baseIndexOf//有没有传递自定义比较器的处理 const length = values.length//要删除的元素的长度 let index = -1//循环索引 let seen = array if (array === values) {//如果array和values严格相等,就复制一份新的values,以防array和values被同时改变 values = copyArray(values) } if (iteratee) { seen = map(array, (value) => iteratee(value))//用迭代器处理array生成新的数组 } while (++index < length) {//循环values查找相同的元素然后splice剪切掉 let fromIndex = 0 const value = values[index]//需要查找的要被删除的值 const computed = iteratee ? iteratee(value) : value//迭代器处理当前要被删除的值 while ((fromIndex = indexOf(seen, computed, fromIndex, comparator)) > -1) { //用baseIndexOf查找computed在seen中的位置,如果找到了,就从seen和array里都删除掉 if (seen !== array) { seen.splice(fromIndex, 1) } array.splice(fromIndex, 1) } } return array } export default basePullAll
map
/** * Creates an array of values by running each element of `array` thru `iteratee`. * The iteratee is invoked with three arguments: (value, index, array). * * @since 5.0.0 * @category Array * @param {Array} array The array to iterate over. * @param {Function} iteratee The function invoked per iteration. * @returns {Array} Returns the new mapped array. * @example * * function square(n) { * return n * n * } * * map([4, 8], square) * // => [16, 64] */ //对数组每一个元素执行迭代器后返回由返回值组成的新数组 function map(array, iteratee) { let index = -1//循环索引 const length = array == null ? 0 : array.length//数组长度 const result = new Array(length)//结果数组 while (++index < length) {//循环调用迭代器,把处理后的返回值存入结果数组 result[index] = iteratee(array[index], index, array) } return result } export default map
baseIndexOf
import baseFindIndex from './baseFindIndex.js' import baseIsNaN from './baseIsNaN.js'//判断一个值是不是NaN import strictIndexOf from './strictIndexOf.js' /** * The base implementation of `indexOf` without `fromIndex` bounds checks. * * @private * @param {Array} array The array to inspect. * @param {*} value The value to search for. * @param {number} fromIndex The index to search from. * @returns {number} Returns the index of the matched value, else `-1`. */ //寻找数组中指定值的位置,找不到返回-1,fromIndex是查找起始位置 function baseIndexOf(array, value, fromIndex) { return value === value ? strictIndexOf(array, value, fromIndex) : baseFindIndex(array, baseIsNaN, fromIndex) //如果value是NaN就调用baseFindIndex查找,传入的回调函数是自己实现的isNaN //如果value不是NaN就调用strictIndexOf查找 } export default baseIndexOf
baseIndexOfWith
/** * This function is like `baseIndexOf` except that it accepts a comparator. * * @private * @param {Array} array The array to inspect. * @param {*} value The value to search for. * @param {number} fromIndex The index to search from. * @param {Function} comparator The comparator invoked per element. * @returns {number} Returns the index of the matched value, else `-1`. */ //类似baseIndexOf,区别是baseIndexOfWith需要提供自定义的比较方法 function baseIndexOfWith(array, value, fromIndex, comparator) { let index = fromIndex - 1//起始查找位置 const { length } = array//数组长度 while (++index < length) {//循环数组,用自定义比较方法比较value和数组当前元素,如果相等,就返回索引 if (comparator(array[index], value)) { return index } } return -1//找不到返回-1 } export default baseIndexOfWith
copyArray
/** * Copies the values of `source` to `array`. * * @private * @param {Array} source The array to copy values from. * @param {Array} [array=[]] The array to copy values to. * @returns {Array} Returns `array`. */ //复制source数组的值到array里 function copyArray(source, array) { let index = -1//循环索引 const length = source.length//source数组长度 array || (array = new Array(length))//如果没有array参数,就新建一个和source长度一样的数组作为array while (++index < length) {//循环source,复制source的元素到array里 array[index] = source[index] } return array//返回array } export default copyArray