Lodash pullAt Return Value Explained

The _.pullAt method in the Lodash JavaScript library removes elements from a given array at specified index positions and mutates that original array. When invoked, _.pullAt returns a new array containing the elements that were removed from the original array, ordered according to the sequence of indices provided.

Return Value and Mutation Behavior

When you use _.pullAt(array, [indexes]), two distinct actions occur:

  1. Array Mutation: The target array passed as the first argument is modified directly in place. The elements at the targeted indices are spliced out, shrinking the length of the original array.
  2. Returned Array: The method returns an entirely new array composed solely of the extracted (removed) elements.

If none of the specified indices exist within the array, the original array remains unchanged, and _.pullAt returns an empty array ([]).

Code Example

const _ = require('lodash');

// Define the initial array
const fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];

// Remove elements at indices 1 and 3 ('banana' and 'date')
const removedElements = _.pullAt(fruits, [1, 3]);

// The return value contains the removed items
console.log(removedElements);
// Output: ['banana', 'date']

// The original array has been mutated in place
console.log(fruits);
// Output: ['apple', 'cherry', 'elderberry']

Key Characteristics of the Return Value