_.without(array, [values])

57

_.without(array, [values])

_.without排除掉数组中存在的给定值,返回一个新数组,使用SameValueZero规则来判断元素是否相等

参数

array (Array): 需要去除元素的数组
[values] (...*): 需要去除的元素组成的数组

返回值

(Array): 返回过滤掉指定元素的新数组

例子

_.without([2, 1, 2, 3], 1, 2);
// => [3]

源代码:

import baseDifference from './.internal/baseDifference.js'
import isArrayLikeObject from './isArrayLikeObject.js'

/**
 * Creates an array excluding all given values using
 * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
 * for equality comparisons.
 *
 * **Note:** Unlike `pull`, this method returns a new array.
 *
 * @since 0.1.0
 * @category Array
 * @param {Array} array The array to inspect.
 * @param {...*} [values] The values to exclude.
 * @returns {Array} Returns the new array of filtered values.
 * @see difference, union, unionBy, unionWith, xor, xorBy, xorWith
 * @example
 *
 * without([2, 1, 2, 3], 1, 2)
 * // => [3]
 */
//排除掉数组中存在的给定值,返回一个新数组,使用SameValueZero规则来判断元素是否相等
function without(array, ...values) {
  return isArrayLikeObject(array) ? baseDifference(array, values) : []
  //如果array是array-like对象,调用baseDifference处理,否则返回空数组
}

export default without

 

posted @ 2018-10-24 18:44  hahazexia  阅读(963)  评论(0)    收藏  举报