_.pullAllBy(array, values, [iteratee=_.identity])
30
_.pullAllBy(array, values, [iteratee=_.identity])
_.pullAllBy和pullAll方法类似,区别是传递一个迭代器iteratee在比较的时候处理array和values数组的每一个元素
参数
array (Array): 需要修改的数组
values (Array): 需要删除的元素的数组
[iteratee=_.identity] (Function): 处理每个元素的迭代器
返回值
(Array):返回删除元素后的原数组
例子
var array = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }]; _.pullAllBy(array, [{ 'x': 1 }, { 'x': 3 }], 'x'); console.log(array); // => [{ 'x': 2 }]
源代码:
源代码和pullAll大体一样,所以细节省略
import basePullAll from './.internal/basePullAll.js' /** * This method is like `pullAll` except that it accepts `iteratee` which is * invoked for each element of `array` and `values` to generate the criterion * by which they're compared. The iteratee is invoked with one argument: (value). * * **Note:** Unlike `differenceBy`, this method mutates `array`. * * @since 4.0.0 * @category Array * @param {Array} array The array to modify. * @param {Array} values The values to remove. * @param {Function} iteratee The iteratee invoked per element. * @returns {Array} Returns `array`. * @see pull, pullAll, pullAllWith, pullAt, remove, reject * @example * * const array = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }] * * pullAllBy(array, [{ 'x': 1 }, { 'x': 3 }], 'x') * console.log(array) * // => [{ 'x': 2 }] */ //和pullAll方法类似,区别是传递一个迭代器iteratee在比较的时候处理array和values数组的每一个元素 function pullAllBy(array, values, iteratee) { return (array != null && array.length && values != null && values.length) ? basePullAll(array, values, iteratee) : array //如果array不是null,并且array有元素,并且values不为null,并且values有元素,就调用basePullAll //否则返回原数组 } export default pullAllBy

浙公网安备 33010602011771号