An alternative to reduce that transforms objects to new accumulator objects through iterative processing
npx @tessl/cli install tessl/npm-lodash--transform@4.6.0Lodash 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.
npm install lodashimport _ from "lodash";
// Use: _.transform(...)ES6 with destructuring:
import _ from "lodash";
const { transform } = _;CommonJS:
const _ = require("lodash");
// Use: _.transform(...) or const { transform } = _;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'] }The transform function implements several key design patterns that make it flexible and powerful:
getIteratee function to normalize the iteratee parameter, allowing for function shorthand, property paths, and object matching patternsarrayEach or baseForOwn to handle both arrays and objects with the same interfacefalse to stop iteration early, providing performance benefits for search operationsAn 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 overiteratee (Function, optional): The function invoked per iteration (defaults to identity function)accumulator (*, optional): The custom accumulator valueIteratee Function:
The iteratee function receives four arguments:
accumulator: The current accumulator value (can be mutated)value: The current element valuekey: The current element key/indexobject: The original object being transformedReturns:
*: The accumulated valueBehavior:
false{}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)