or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash-pullat

Array utility function that removes elements at specified indexes and returns the removed elements

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.pullat@4.6.x

To install, run

npx @tessl/cli install tessl/npm-lodash-pullat@4.6.0

index.mddocs/

Lodash pullAt

Lodash pullAt is an array utility function that removes elements from an array at specified indexes and returns the removed elements. Unlike _.at, this method mutates the original array. It handles indexes flexibly, accepting them individually or as arrays.

Package Information

  • Package Name: lodash
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash

Core Imports

import _ from "lodash";

For modular usage:

import pullAt from "lodash.pullat";

CommonJS:

const _ = require("lodash");
// or  
const pullAt = require("lodash.pullat");

Basic Usage

import _ from "lodash";

// Remove elements at specific indexes
const array = [5, 10, 15, 20];
const removed = _.pullAt(array, 1, 3);

console.log(array);   // => [5, 15] (original array is mutated)
console.log(removed); // => [10, 20] (removed elements are returned)

// Using array of indexes
const data = ['a', 'b', 'c', 'd', 'e'];
const extracted = _.pullAt(data, [0, 2, 4]);

console.log(data);      // => ['b', 'd'] (mutated)
console.log(extracted); // => ['a', 'c', 'e'] (extracted elements)

Capabilities

Array Element Removal

Removes elements from array corresponding to indexes and returns an array of removed elements.

/**
 * Removes elements from `array` corresponding to `indexes` and returns an
 * array of removed elements.
 *
 * Note: Unlike `_.at`, this method mutates `array`.
 *
 * @param {Array} array The array to modify.
 * @param {...(number|number[])} [indexes] The indexes of elements to remove,
 *  specified individually or in arrays.
 * @returns {Array} Returns the new array of removed elements.
 */
function pullAt(array, ...indexes);

Parameters:

  • array (Array): The array to modify
  • ...indexes (...(number|number[])): The indexes of elements to remove, specified individually or in arrays

Returns:

  • Array: Returns the new array of removed elements

Behavior:

  • Mutating: The original array is modified in place
  • Flexible Index Input: Accepts indexes as individual arguments or nested arrays
  • Index Processing: Indexes are flattened and sorted before processing to ensure correct removal
  • Return Value: Returns an array containing the elements that were removed

Usage Examples:

// Individual indexes
const arr1 = [1, 2, 3, 4, 5];
const removed1 = _.pullAt(arr1, 0, 2, 4);
// arr1: [2, 4]
// removed1: [1, 3, 5]

// Array of indexes  
const arr2 = ['x', 'y', 'z', 'a', 'b'];
const removed2 = _.pullAt(arr2, [1, 3]);
// arr2: ['x', 'z', 'b']  
// removed2: ['y', 'a']

// Mixed individual and array indexes
const arr3 = [10, 20, 30, 40, 50, 60];
const removed3 = _.pullAt(arr3, 0, [2, 4], 5);
// arr3: [20, 40]
// removed3: [10, 30, 50, 60]

// Empty array handling
const arr4 = [];
const removed4 = _.pullAt(arr4, 0, 1);
// arr4: []
// removed4: []

// Out of bounds indexes (safely ignored)
const arr5 = [1, 2, 3];
const removed5 = _.pullAt(arr5, 1, 10, 20);
// arr5: [1, 3]
// removed5: [2]

Access Patterns

Full Library Access

When using the complete lodash library:

import _ from "lodash";
const result = _.pullAt(array, ...indexes);

Modular Access

When using the standalone pullAt package:

// Install: npm install lodash.pullat
import pullAt from "lodash.pullat";
const result = pullAt(array, ...indexes);

// CommonJS
const pullAt = require("lodash.pullat");
const result = pullAt(array, ...indexes);

Browser CDN Access

<script src="https://cdn.jsdelivr.net/npm/lodash@4.6.0/lodash.min.js"></script>
<script>
  const result = _.pullAt(array, ...indexes);
</script>

Related Functions

These lodash functions provide similar array manipulation capabilities:

  • _.pull(array, ...values) - Removes specified values from array
  • _.pullAll(array, values) - Removes array of values from array
  • _.pullAllBy(array, values, iteratee) - Removes values using iteratee
  • _.pullAllWith(array, values, comparator) - Removes values using comparator
  • _.remove(array, predicate) - Removes elements matching predicate
  • _.at(object, ...paths) - Gets values at specified paths (non-mutating)

Implementation Notes

  • Uses internal rest function to create variadic parameter handling
  • Indexes are flattened using baseFlatten and converted to strings
  • Uses baseAt to extract elements before removal
  • Uses basePullAt for the actual array mutation with sorted indexes
  • Indexes are sorted in ascending order before processing to maintain correct removal sequence