A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-transform-method/ below:

Lodash _.transform() Method - GeeksforGeeks

Lodash _.transform() Method

Last Updated : 15 Jul, 2025

Lodash _.transform() method is an alternative to the _.reduce() method transforms an object to a new accumulator object which is the result of running each of its own enumerable string keyed properties through iteratee with each invocation potentially mutating the accumulator object. A new object with the same prototype will be used if an accumulator is not provided. 

Syntax:  
_.transform(object, iteratee, accumulator);
Parameters:  Return Value:

This method returns the accumulated value.

Example 1: In this example, we are transforming the given array by the use of the lodash _.transform() method.

javascript
// Requiring the lodash library 
const _ = require("lodash");

// Original array 
let object = [12, 13, 14];

// Using the _.transform() method
let st_elem = _.transform(object,
    function (result, n) {
        result.push(n *= n);
        return n % 2 == 0;
    }, []);

// Printing the output 
console.log(st_elem);

Output:  

144, 169

Example 2: In this example, we are transforming the given array by the use of the lodash _.transform() method.

javascript
// Requiring the lodash library 
const _ = require("lodash");

// Original array 
let object = { 'p': 3, 'q': 6, 'r': 3 };

// Using the _.transform() method
let st_elem = _.transform(object,
    function (result, value, key) {
        (result[value] || (result[value] = [])).push(key);
    }, {});

// Printing the output 
console.log(st_elem);

Output: 

{ '3': ['p', 'r'], '6': ['q'] }

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4