_.pullAt(array, [indexes])
32
_.pullAt(array, [indexes])
_pullAt删除数组对应索引的元素,然后返回被修改的原数组
参数
array (Array): 需要修改的数组
[indexes] (...(number|number[])): 要被删除的元素的索引组成的数组
返回值
(Array): 返回删除元素后的原数组
例子
var array = ['a', 'b', 'c', 'd']; var pulled = _.pullAt(array, [1, 3]); console.log(array); // => ['a', 'c'] console.log(pulled); // => ['b', 'd']
源代码:
很多细节以前已经涉及到了,省略
import map from './map.js' import baseAt from './.internal/baseAt.js' import basePullAt from './.internal/basePullAt.js' import compareAscending from './.internal/compareAscending.js' import isIndex from './.internal/isIndex.js' /** * Removes elements from `array` corresponding to `indexes` and returns an * array of removed elements. * * **Note:** Unlike `at`, this method mutates `array`. * * @since 3.0.0 * @category Array * @param {Array} array The array to modify. * @param {...(number|number[])} [indexes] The indexes of elements to remove. * @returns {Array} Returns the new array of removed elements. * @see pull, pullAll, pullAllBy, pullAllWith, remove, reject * @example * * const array = ['a', 'b', 'c', 'd'] * const pulled = pullAt(array, [1, 3]) * * console.log(array) * // => ['a', 'c'] * * console.log(pulled) * // => ['b', 'd'] */ //删除数组对应索引的元素,然后返回被修改的原数组 function pullAt(array, ...indexes) { const length = array == null ? 0 : array.length//数组长度 const result = baseAt(array, indexes)//调用baseAt,根据索引数组获取到array上对应的元素存成一个新数组返回 basePullAt(array, map(indexes, (index) => isIndex(index, length) ? +index : index).sort(compareAscending)) //调用basePullAt,第一个参数是要删除元素的数组,第二个参数是要删除的元素的数组索引已经排好了顺序 return result//原数组被改变,返回被删除掉的元素组成的数组 } export default pullAt
baseAt
import get from '../get.js' /** * The base implementation of `at` without support for individual paths. * * @private * @param {Object} object The object to iterate over. * @param {string[]} paths The property paths to pick. * @returns {Array} Returns the picked elements. */ //at方法的基础实现 //object需要迭代的对象,paths属性路径数组 function baseAt(object, paths) { let index = -1//循环索引 const length = paths.length//paths的长度 const result = new Array(length)//结果数组长度和paths一样 const skip = object == null//skip标记是否跳过,如果object是null就要跳过 while (++index < length) {//循环paths result[index] = skip ? undefined : get(object, paths[index])//用get获取到目标元素push入结果数组 } return result } export default baseAt
basePullAt
import baseUnset from './baseUnset.js' import isIndex from './isIndex.js' /** * The base implementation of `pullAt` without support for individual * indexes or capturing the removed elements. * * @private * @param {Array} array The array to modify. * @param {number[]} indexes The indexes of elements to remove. * @returns {Array} Returns `array`. */ //pullAt的基础实现 function basePullAt(array, indexes) { let length = array ? indexes.length : 0//要删除的索引的数组的长度 const lastIndex = length - 1//indexes最后一个元素索引 while (length--) {//循环indexed数组 let previous const index = indexes[length]//当前要删除的元素的索引 if (length == lastIndex || index !== previous) {//如果是第一次删除或者和前一个索引不一样,就执行删除操作 previous = index//存下上一个索引 if (isIndex(index)) {//如果index是合法索引,array里删除对应的元素 array.splice(index, 1) } else {//如果index不是合法索引,调用baseUnset处理 baseUnset(array, index) } } } return array } export default basePullAt
baseUnset
import castPath from './castPath.js' import last from '../last.js' import parent from './parent.js' import toKey from './toKey.js' /** * The base implementation of `unset`. * * @private * @param {Object} object The object to modify. * @param {Array|string} path The property path to unset. * @returns {boolean} Returns `true` if the property is deleted, else `false`. */ //unset的基础实现,从对象身上移除指定属性 function baseUnset(object, path) { path = castPath(path, object)//将path处理成路径数组 object = parent(object, path)//根据对象的路径数组获取到倒数第二个父级值 return object == null || delete object[toKey(last(path))] //删除倒数第二个父级值身上的指定属性值 } export default baseUnset

浙公网安备 33010602011771号