@@ -10,13 +10,23 @@ Functional methods like forEach, map, filter, and other Array methods for Object
10
10
# Index
11
11
12
12
[ chain] ( https://github.com/tjmehta/object-loops#usage )
13
+
13
14
[ every] ( https://github.com/tjmehta/object-loops#every )
14
- [ find] ( https://github.com/tjmehta/object-loops#find )
15
- [ findKey] ( https://github.com/tjmehta/object-loops#findKey )
15
+
16
16
[ filter] ( https://github.com/tjmehta/object-loops#filter )
17
+
18
+ [ findKey] ( https://github.com/tjmehta/object-loops#findKey )
19
+
20
+ [ find] ( https://github.com/tjmehta/object-loops#find )
21
+
17
22
[ forEach] ( https://github.com/tjmehta/object-loops#forEach )
23
+
24
+ [ mapKeys] ( https://github.com/tjmehta/object-loops#mapKeys )
25
+
18
26
[ map] ( https://github.com/tjmehta/object-loops#map )
27
+
19
28
[ reduce] ( https://github.com/tjmehta/object-loops#reduce )
29
+
20
30
[ some] ( https://github.com/tjmehta/object-loops#some )
21
31
22
32
# Usage
@@ -88,6 +98,35 @@ allGreaterThan25 // false
88
98
*/
89
99
```
90
100
101
+ ## filter
102
+
103
+ Creates a new object with all entries that pass the test implemented by the provided function.
104
+
105
+ * @param {object} [ obj] - object to filter values, not accepted if being used directly on Object.prototype
106
+ * @param {function} callback - function to test each value in the object. return true to keep that entry, false otherwise.
107
+ * @param {* } [ thisArg] - optional. context to bind to callback
108
+ * @returns {object} newly created object with filtered values
109
+
110
+ ``` js
111
+ var filter = require (' object-loops/filter' )
112
+
113
+ var obj = {
114
+ foo: 10 ,
115
+ bar: 20 ,
116
+ baz: 30 ,
117
+ qux: 40 ,
118
+ }
119
+ var filteredObj = filter (obj, function (val , key , obj ) {
120
+ return val > 25
121
+ })
122
+ filteredObj /* Only has entries with vals greater than 25
123
+ {
124
+ baz: 30,
125
+ qux: 40
126
+ }
127
+ */
128
+ ```
129
+
91
130
## find
92
131
93
132
Find the value of the the object that passes the test implemented by the callback.
@@ -146,59 +185,59 @@ notfound // undefined
146
185
*/
147
186
```
148
187
149
- ## filter
188
+ ## forEach
150
189
151
- Creates a new object with all entries that pass the test implemented by the provided function .
190
+ Executes a provided function once per each object value .
152
191
153
- * @param {object} [ obj] - object to filter values , not accepted if being used directly on Object.prototype
154
- * @param {function} callback - function to test each value in the object. return true to keep that entry, false otherwise.
192
+ * @param {object} [ obj] - object to forEach , not accepted if being used directly on Object.prototype
193
+ * @param {function} callback - function that will be invoked once for each key-value pair
155
194
* @param {* } [ thisArg] - optional. context to bind to callback
156
- * @returns {object} newly created object with filtered values
157
195
158
196
``` js
159
- var filter = require (' object-loops/filter ' )
197
+ var forEach = require (' object-loops/for-each ' )
160
198
161
199
var obj = {
162
200
foo: 10 ,
163
201
bar: 20 ,
164
- baz: 30 ,
165
- qux: 40 ,
202
+ baz: 30
166
203
}
167
- var filteredObj = filter (obj, function (val , key , obj ) {
168
- return val > 25
204
+ var keyConcat = ' '
205
+ var valSum = 0
206
+ forEach (obj, function (val , key , obj ) {
207
+ keyConcat += key
208
+ valSum += val
169
209
})
170
- filteredObj /* Only has entries with vals greater than 25
171
- {
172
- baz: 30,
173
- qux: 40
174
- }
175
- */
210
+ keyConcat // = 'foobarbaz'
211
+ valSum // = 60
176
212
```
177
213
178
- ## forEach
214
+ ## mapKeys
179
215
180
- Executes a provided function once per each object value .
216
+ Creates a new object with the results of calling a provided function on every key in the object .
181
217
182
- * @param {object} [ obj] - object to forEach , not accepted if being used directly on Object.prototype
183
- * @param {function } callback - function that will be invoked once for each key-value pair
218
+ * @param {object} [ obj] - object to map keys , not accepted if being used directly on Object.prototype
219
+ * @param {mapKeysCallback } callback - function that produces the new key for the new mapped object
184
220
* @param {* } [ thisArg] - optional. context to bind to callback
221
+ * @returns {object} newly created object with mapped keys
185
222
186
223
``` js
187
- var forEach = require (' object-loops/for-each ' )
224
+ var mapKeys = require (' object-loops/map-keys ' )
188
225
189
226
var obj = {
190
227
foo: 10 ,
191
228
bar: 20 ,
192
229
baz: 30
193
230
}
194
- var keyConcat = ' '
195
- var valSum = 0
196
- forEach (obj, function (val , key , obj ) {
197
- keyConcat += key
198
- valSum += val
231
+ var mappedObj = mapKeys (obj, function (key , val , obj ) {
232
+ return key + ' New'
199
233
})
200
- keyConcat // = 'foobarbaz'
201
- valSum // = 60
234
+ mappedObj /* Each key is concated w/ 'New'
235
+ {
236
+ fooNew: 10,
237
+ barNew: 20,
238
+ bazNew: 30
239
+ }
240
+ */
202
241
```
203
242
204
243
## map
0 commit comments