or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--transform

An alternative to reduce that transforms objects to new accumulator objects through iterative processing

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

To install, run

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

index.mddocs/

Lodash Transform

Lodash Transform provides an alternative to reduce that transforms objects to new accumulator objects through iterative processing. It iterates over object properties or array elements, allowing for flexible accumulation and transformation patterns with automatic accumulator initialization based on input type.

Package Information

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

Core Imports

import _ from "lodash";
// Use: _.transform(...)

ES6 with destructuring:

import _ from "lodash";
const { transform } = _;

CommonJS:

const _ = require("lodash");
// Use: _.transform(...) or const { transform } = _;

Basic Usage

import _ from "lodash";
const { transform } = _;

// Transform array to new structure
const numbers = transform([2, 3, 4], function(result, n) {
  result.push(n * n);
  return n % 2 == 0; // Exit early when even number found
}, []);
// => [4, 9]

// Transform object to grouped structure  
const users = { 'a': 1, 'b': 2, 'c': 1 };
const grouped = transform(users, function(result, value, key) {
  (result[value] || (result[value] = [])).push(key);
}, {});
// => { '1': ['a', 'c'], '2': ['b'] }

Architecture

The transform function implements several key design patterns that make it flexible and powerful:

  • Iteratee Pattern: Uses lodash's internal getIteratee function to normalize the iteratee parameter, allowing for function shorthand, property paths, and object matching patterns
  • Automatic Accumulator Initialization: When no accumulator is provided, intelligently creates one based on the input type, preserving constructors and prototype chains
  • Unified Array/Object Processing: Internally uses either arrayEach or baseForOwn to handle both arrays and objects with the same interface
  • Early Exit Support: The iteratee can return false to stop iteration early, providing performance benefits for search operations
  • Mutation-Safe Design: While the accumulator can be mutated within the iteratee, the original object remains unchanged

Capabilities

Object Transformation

An alternative to reduce that transforms objects to new accumulator objects through iterative processing.

/**
 * An alternative to _.reduce; this method transforms object to a new 
 * accumulator object which is the result of running each of its own enumerable
 * properties through iteratee, with each invocation potentially mutating
 * the accumulator object. The iteratee is invoked with four arguments:
 * (accumulator, value, key, object). Iteratee functions may exit iteration
 * early by explicitly returning false.
 * 
 * @param {Array|Object} object The object to iterate over.
 * @param {Function} [iteratee=_.identity] The function invoked per iteration.
 * @param {*} [accumulator] The custom accumulator value.
 * @returns {*} Returns the accumulated value.
 */
function transform(object, iteratee, accumulator);

Parameters:

  • object (Array|Object): The object to iterate over
  • iteratee (Function, optional): The function invoked per iteration (defaults to identity function)
  • accumulator (*, optional): The custom accumulator value

Iteratee Function:

The iteratee function receives four arguments:

  1. accumulator: The current accumulator value (can be mutated)
  2. value: The current element value
  3. key: The current element key/index
  4. object: The original object being transformed

Returns:

  • *: The accumulated value

Behavior:

  • Iteratee can exit iteration early by explicitly returning false
  • If no accumulator provided, creates appropriate default:
    • Arrays/TypedArrays: Creates new array with same constructor
    • Objects: Creates plain object or preserves prototype chain
    • Other values: Creates empty object {}
  • Handles sparse arrays as dense arrays (undefined values become 'undefined')
  • Works with both arrays and objects seamlessly

Usage Examples:

import _ from "lodash";
const { transform } = _;
// Array transformation with early exit
const result1 = transform([2, 3, 4], function(result, n) {
  result.push(n * n);
  return n % 2 == 0; // Stop after finding first even number
}, []);
// => [4, 9]

// Object property grouping
const result2 = transform({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
  (result[value] || (result[value] = [])).push(key);
}, {});
// => { '1': ['a', 'c'], '2': ['b'] }

// Auto-accumulator creation for arrays
const result3 = transform([1, 2, 3], function(result, value, index) {
  result[index] = value * 2;
});
// => [2, 4, 6]

// Auto-accumulator creation for objects  
const result4 = transform({ a: 1, b: 2 }, function(result, value, key) {
  result[key.toUpperCase()] = value;
});
// => { A: 1, B: 2 }

// Constructor preservation for typed arrays
const typedArray = new Int32Array([1, 2, 3]);
const result5 = transform(typedArray, function(result, value, index) {
  result[index] = value * 2;
});
// => Int32Array [2, 4, 6]

// Working with custom constructors
function Person(name) { this.name = name; }
Person.prototype.greet = function() { return 'Hello!'; };
const person = new Person('John');

const result6 = transform(person, function(result, value, key) {
  if (key !== 'name') return;
  result[key] = value.toUpperCase();
});
// => Person { name: 'JOHN' } (preserves prototype)