or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

tessl/npm-lodash--reduce

Lodash reduce functionality for transforming arrays and objects by applying an iteratee function to accumulate results

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

To install, run

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

0

# Lodash Reduce

1

2

Lodash reduce functionality provides enhanced collection reduction capabilities for both arrays and objects. It transforms collections by applying an iteratee function to accumulate results, offering improved performance, consistent behavior across data types, and powerful iteratee shorthand support beyond native JavaScript reduce.

3

4

## Package Information

5

6

- **Package Name**: lodash

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install lodash`

10

- **Version**: 4.6.0

11

12

## Core Imports

13

14

```javascript

15

// Full lodash import

16

const _ = require('lodash');

17

```

18

19

ES6 Import:

20

```javascript

21

import _ from 'lodash';

22

```

23

24

Standalone package (if available):

25

```javascript

26

const reduce = require('lodash.reduce');

27

```

28

29

## Basic Usage

30

31

```javascript

32

const _ = require('lodash');

33

34

// Array reduction with initial accumulator

35

const sum = _.reduce([1, 2, 3, 4], function(total, n) {

36

return total + n;

37

}, 0);

38

// => 10

39

40

// Object reduction without initial accumulator

41

const flattened = _.reduce([[0, 1], [2, 3], [4, 5]], function(flat, item) {

42

return flat.concat(item);

43

});

44

// => [0, 1, 2, 3, 4, 5]

45

46

// Object transformation and grouping

47

const grouped = _.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {

48

(result[value] || (result[value] = [])).push(key);

49

return result;

50

}, {});

51

// => { '1': ['a', 'c'], '2': ['b'] }

52

```

53

54

## Capabilities

55

56

### Array and Object Reduction

57

58

Reduces collections to a single value by applying an iteratee function that accumulates results through each element.

59

60

```javascript { .api }

61

/**

62

* Reduces collection to a value which is the accumulated result of running

63

* each element through iteratee, where each successive invocation is supplied

64

* the return value of the previous. If accumulator is not given, the first

65

* element of collection is used as the initial value.

66

*

67

* @param {Array|Object} collection - The collection to iterate over

68

* @param {Function} [iteratee=_.identity] - The function invoked per iteration

69

* @param {*} [accumulator] - The initial value

70

* @returns {*} Returns the accumulated value

71

*/

72

_.reduce(collection, iteratee, accumulator);

73

```

74

75

**Iteratee Arguments:**

76

- `accumulator` - The accumulated value

77

- `value` - The current element value

78

- `index|key` - The current element index (arrays) or key (objects)

79

- `collection` - The collection being iterated

80

81

**Supported Collection Types:**

82

- Arrays and array-like objects

83

- Plain objects

84

- Typed arrays

85

- Strings (treated as character arrays)

86

87

### Right-to-Left Reduction

88

89

Similar to reduce but iterates over elements from right to left, useful for operations where order matters.

90

91

```javascript { .api }

92

/**

93

* This method is like _.reduce except that it iterates over elements of

94

* collection from right to left.

95

*

96

* @param {Array|Object} collection - The collection to iterate over

97

* @param {Function} [iteratee=_.identity] - The function invoked per iteration

98

* @param {*} [accumulator] - The initial value

99

* @returns {*} Returns the accumulated value

100

*/

101

_.reduceRight(collection, iteratee, accumulator);

102

```

103

104

**Usage Examples:**

105

106

```javascript

107

// Right-to-left array processing

108

const reversed = _.reduceRight([1, 2, 3, 4], function(accumulator, value) {

109

return accumulator.concat(value);

110

}, []);

111

// => [4, 3, 2, 1]

112

113

// Right-to-left object processing

114

const rightToLeft = _.reduceRight({ 'a': 1, 'b': 2, 'c': 3 }, function(result, value, key) {

115

result.push(key + value);

116

return result;

117

}, []);

118

// => ['c3', 'b2', 'a1'] (order may vary in older environments)

119

```

120

121

### Iteratee Shorthand Support

122

123

Lodash reduce supports powerful iteratee shorthands for common operations through the iteratee system.

124

125

```javascript { .api }

126

// Property name shorthand

127

_.reduce(objects, 'property', initialValue);

128

129

// Property path shorthand

130

_.reduce(objects, 'nested.property', initialValue);

131

132

// Partial object matching

133

_.reduce(objects, { 'property': value }, initialValue);

134

```

135

136

**Integration with Guarded Methods:**

137

Many lodash methods work as iteratees for reduce operations:

138

- `assign`, `defaults`, `defaultsDeep`

139

- `includes`, `merge`, `orderBy`, `sortBy`

140

141

```javascript

142

// Using guarded methods as iteratees

143

const merged = _.reduce([obj1, obj2, obj3], _.assign, {});

144

```

145

146

## Error Handling

147

148

### Empty Collections

149

- **With accumulator**: Returns the accumulator value

150

- **Without accumulator**: Returns `undefined`

151

152

### Invalid Collections

153

- **null/undefined**: Treated as empty collection, returns accumulator or `undefined`

154

- **Primitive values**: Numbers, booleans converted to objects for iteration

155

156

### Type Safety

157

- All type checking handled internally

158

- Consistent behavior across different JavaScript environments

159

- Performance optimizations for common data types

160

161

## Performance Characteristics

162

163

### Optimization Strategies

164

- **Array Fast Path**: Specialized `arrayReduce` implementation for better performance

165

- **Object Fast Path**: Optimized iteration for plain objects

166

- **Type Detection**: Runtime optimization based on collection type

167

- **Early Exit**: Efficient handling of edge cases

168

169

### Memory Efficiency

170

- Single-pass iteration

171

- No intermediate array creation

172

- Minimal memory overhead for large collections

173

174

## Integration Features

175

176

### Method Chaining

177

Full compatibility with lodash's method chaining system:

178

179

```javascript

180

_([1, 2, 3, 4])

181

.map(n => n * 2)

182

.reduce((sum, n) => sum + n, 0)

183

.value();

184

// => 20

185

```

186

187

### Functional Programming

188

Auto-curried versions available in lodash/fp:

189

190

```javascript

191

const fp = require('lodash/fp');

192

193

const sumNumbers = fp.reduce((sum, n) => sum + n, 0);

194

sumNumbers([1, 2, 3]); // => 6

195

```

196

197

### Custom Iteratee Integration

198

Respects global `_.iteratee` customization:

199

200

```javascript

201

// Custom iteratee behavior affects reduce operations

202

_.iteratee = customIterateeFunction;

203

_.reduce(collection, shorthand); // Uses custom iteratee resolution

204

```