or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

array-functions.mdcollection-functions.mdfunction-utilities.mdindex.mdobject-functions.mdtype-checking.md
tile.json

collection-functions.mddocs/

0

# Collection Functions

1

2

Powerful iteration and data processing functions that work with arrays, objects, and strings. These functions form the core of functional programming patterns and data transformation workflows.

3

4

## Capabilities

5

6

### Map

7

8

Creates an array of values by running each element in collection through iteratee.

9

10

```javascript { .api }

11

/**

12

* Creates an array of values by running each element in `collection` through

13

* `iteratee`. The `iteratee` is bound to `thisArg` and invoked with three

14

* arguments: (value, index|key, collection).

15

*

16

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

17

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

18

* @param {*} [thisArg] The `this` binding of `iteratee`.

19

* @returns {Array} Returns the new mapped array.

20

*/

21

function map(collection, iteratee, thisArg);

22

```

23

24

**Usage Examples:**

25

26

```javascript

27

var map = require('lodash.map');

28

29

// Transform numbers

30

map([1, 2, 3, 4], function(n) { return n * 2; });

31

// => [2, 4, 6, 8]

32

33

// Extract property values

34

var users = [{ name: 'alice' }, { name: 'bob' }];

35

map(users, 'name');

36

// => ['alice', 'bob']

37

38

// Work with objects

39

map({ a: 1, b: 2 }, function(value, key) { return key + value; });

40

// => ['a1', 'b2']

41

```

42

43

### Filter

44

45

Iterates over elements of collection, returning an array of all elements predicate returns truthy for.

46

47

```javascript { .api }

48

/**

49

* Iterates over elements of `collection`, returning an array of all elements

50

* `predicate` returns truthy for. The predicate is bound to `thisArg` and

51

* invoked with three arguments: (value, index|key, collection).

52

*

53

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

54

* @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.

55

* @param {*} [thisArg] The `this` binding of `predicate`.

56

* @returns {Array} Returns the new filtered array.

57

*/

58

function filter(collection, predicate, thisArg);

59

```

60

61

**Usage Examples:**

62

63

```javascript

64

var filter = require('lodash.filter');

65

66

// Filter numbers

67

filter([1, 2, 3, 4, 5, 6], function(n) { return n % 2 === 0; });

68

// => [2, 4, 6]

69

70

// Filter objects by property

71

var users = [

72

{ name: 'alice', active: true },

73

{ name: 'bob', active: false }

74

];

75

filter(users, { active: true });

76

// => [{ name: 'alice', active: true }]

77

78

filter(users, 'active');

79

// => [{ name: 'alice', active: true }]

80

```

81

82

### Reduce

83

84

Reduces collection to a value which is the accumulated result of running each element through iteratee.

85

86

```javascript { .api }

87

/**

88

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

89

* each element in `collection` through `iteratee`, where each successive

90

* invocation is supplied the return value of the previous.

91

*

92

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

93

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

94

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

95

* @param {*} [thisArg] The `this` binding of `iteratee`.

96

* @returns {*} Returns the accumulated value.

97

*/

98

function reduce(collection, iteratee, accumulator, thisArg);

99

```

100

101

**Usage Examples:**

102

103

```javascript

104

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

105

106

// Sum numbers

107

reduce([1, 2, 3], function(sum, n) { return sum + n; }, 0);

108

// => 6

109

110

// Group by property

111

reduce([1.3, 2.1, 2.4], function(result, n) {

112

var key = Math.floor(n);

113

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

114

return result;

115

}, {});

116

// => { '1': [1.3], '2': [2.1, 2.4] }

117

```

118

119

### Find

120

121

Iterates over elements of collection, returning the first element predicate returns truthy for.

122

123

```javascript { .api }

124

/**

125

* Iterates over elements of `collection`, returning the first element

126

* `predicate` returns truthy for. The predicate is bound to `thisArg` and

127

* invoked with three arguments: (value, index|key, collection).

128

*

129

* @param {Array|Object|string} collection The collection to search.

130

* @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.

131

* @param {*} [thisArg] The `this` binding of `predicate`.

132

* @returns {*} Returns the matched element, else `undefined`.

133

*/

134

function find(collection, predicate, thisArg);

135

```

136

137

**Usage Examples:**

138

139

```javascript

140

var find = require('lodash.find');

141

142

var users = [

143

{ name: 'alice', age: 25 },

144

{ name: 'bob', age: 30 },

145

{ name: 'charlie', age: 35 }

146

];

147

148

// Find by function

149

find(users, function(user) { return user.age > 30; });

150

// => { name: 'charlie', age: 35 }

151

152

// Find by property match

153

find(users, { name: 'bob' });

154

// => { name: 'bob', age: 30 }

155

156

// Find by property value

157

find(users, 'name', 'alice');

158

// => { name: 'alice', age: 25 }

159

```

160

161

### Each (forEach)

162

163

Iterates over elements of collection invoking iteratee for each element.

164

165

```javascript { .api }

166

/**

167

* Iterates over elements of `collection` invoking `iteratee` for each element.

168

* The `iteratee` is bound to `thisArg` and invoked with three arguments:

169

* (value, index|key, collection).

170

*

171

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

172

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

173

* @param {*} [thisArg] The `this` binding of `iteratee`.

174

* @returns {Array|Object|string} Returns `collection`.

175

*/

176

function forEach(collection, iteratee, thisArg);

177

```

178

179

### Every

180

181

Checks if predicate returns truthy for all elements of collection.

182

183

```javascript { .api }

184

/**

185

* Checks if `predicate` returns truthy for **all** elements of `collection`.

186

* The predicate is bound to `thisArg` and invoked with three arguments:

187

* (value, index|key, collection).

188

*

189

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

190

* @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.

191

* @param {*} [thisArg] The `this` binding of `predicate`.

192

* @returns {boolean} Returns `true` if all elements pass the predicate check, else `false`.

193

*/

194

function every(collection, predicate, thisArg);

195

```

196

197

### Some

198

199

Checks if predicate returns truthy for any element of collection.

200

201

```javascript { .api }

202

/**

203

* Checks if `predicate` returns truthy for **any** element of `collection`.

204

* The predicate is bound to `thisArg` and invoked with three arguments:

205

* (value, index|key, collection).

206

*

207

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

208

* @param {Function|Object|string} [predicate=_.identity] The function invoked per iteration.

209

* @param {*} [thisArg] The `this` binding of `predicate`.

210

* @returns {boolean} Returns `true` if any element passes the predicate check, else `false`.

211

*/

212

function some(collection, predicate, thisArg);

213

```

214

215

### GroupBy

216

217

Creates an object composed of keys generated from the results of running each element through iteratee.

218

219

```javascript { .api }

220

/**

221

* Creates an object composed of keys generated from the results of running

222

* each element of `collection` through `iteratee`. The corresponding value

223

* of each key is an array of the elements responsible for generating the key.

224

*

225

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

226

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

227

* @param {*} [thisArg] The `this` binding of `iteratee`.

228

* @returns {Object} Returns the composed aggregate object.

229

*/

230

function groupBy(collection, iteratee, thisArg);

231

```

232

233

**Usage Examples:**

234

235

```javascript

236

var groupBy = require('lodash.groupby');

237

238

// Group by length

239

groupBy(['one', 'two', 'three'], 'length');

240

// => { '3': ['one', 'two'], '5': ['three'] }

241

242

// Group by Math.floor

243

groupBy([6.1, 4.2, 6.3], function(n) { return Math.floor(n); });

244

// => { '4': [4.2], '6': [6.1, 6.3] }

245

```

246

247

### SortBy

248

249

Creates an array of elements, sorted in ascending order by the results of running each element through iteratee.

250

251

```javascript { .api }

252

/**

253

* Creates an array of elements, sorted in ascending order by the results of

254

* running each element in a collection through `iteratee`. This method performs

255

* a stable sort, that is, it preserves the original sort order of equal elements.

256

*

257

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

258

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

259

* @param {*} [thisArg] The `this` binding of `iteratee`.

260

* @returns {Array} Returns the new sorted array.

261

*/

262

function sortBy(collection, iteratee, thisArg);

263

```

264

265

**Usage Examples:**

266

267

```javascript

268

var sortBy = require('lodash.sortby');

269

270

var users = [

271

{ name: 'charlie', age: 40 },

272

{ name: 'alice', age: 30 },

273

{ name: 'bob', age: 35 }

274

];

275

276

// Sort by property

277

sortBy(users, 'age');

278

// => [{ name: 'alice', age: 30 }, { name: 'bob', age: 35 }, { name: 'charlie', age: 40 }]

279

280

// Sort by function

281

sortBy(users, function(user) { return user.name; });

282

// => [{ name: 'alice', age: 30 }, { name: 'bob', age: 35 }, { name: 'charlie', age: 40 }]

283

```

284

285

## Additional Functions

286

287

The collection category also includes: `at`, `countBy`, `findLast`, `findWhere`, `forEachRight`, `includes`, `indexBy`, `invoke`, `max`, `min`, `partition`, `pluck`, `reduceRight`, `reject`, `sample`, `shuffle`, `size`, `where`

288

289

**Package Installation:**

290

291

```bash

292

npm install lodash.map lodash.filter lodash.reduce lodash.find

293

npm install lodash.foreach lodash.every lodash.some lodash.groupby

294

npm install lodash.sortby

295

```