2ffcab10665b4f60ffa1faacdb4e90813ceacf2e
[platform/framework/web/crosswalk-tizen.git] /
1 # collection #
2
3 Methods for dealing with collections (Array or Objects).
4
5
6
7 ## contains(list, value):Boolean
8
9 Checks if collection contains value.
10
11 ```js
12 contains({a: 1, b: 2, c: 'bar'}, 2); // true
13 contains([1, 2, 3], 'foo');  // false
14 ```
15
16 See: [array/contains](array.html#contains), [object/contains](object.html#contains)
17
18
19
20 ## every(list, callback, [thisObj]):Boolean
21
22 Tests whether all values in the collection pass the test implemented by the
23 provided callback.
24
25 ```js
26 var obj = {
27     a: 1,
28     b: 2,
29     c: 3,
30     d: 'string'
31 };
32
33 every(obj, isNumber); // false
34 ```
35
36 See: [array/every](array.html#every), [object/every](object.html#every)
37
38
39
40 ## filter(list, callback, [thisObj]):Array
41
42 Filter collection properties.
43
44 See: [array/filter](array.html#filter), [object/filter](object.html#filter)
45
46
47
48 ## find(list, callback, [thisObj]):*
49
50 Loops through all the values in the collection and returns the first one that
51 passes a truth test (callback).
52
53 **Important:** loop order over objects properties isn't guaranteed to be the
54 same on all environments.
55
56 ```js
57 find({a: 'foo', b: 12}, isString); // 'foo'
58 find(['foo', 12], isNumber); // 12
59 ```
60
61 See: [array/find](array.html#find), [object/find](object.html#find)
62
63
64
65 ## forEach(list, callback, [thisObj])
66
67 Loop through all values of the collection.
68
69 See: [array/forEach](array.html#forEach), [object/forOwn](object.html#forOwn)
70
71
72
73 ## map(list, callback, [thisObj]):Array
74
75 Returns a new collection where the properties values are the result of calling
76 the callback for each property in the original collection.
77
78 See: [array/map](array.html#map), [object/map](object.html#map)
79
80
81
82 ## max(list, [iterator]):*
83
84 Returns maximum value inside collection or use a custom iterator to define how
85 items should be compared.
86
87 See: [`min()`](#min), [array/max](array.html#max), [object/max](object.html#max)
88
89 ```js
90 max({a: 100, b: 2, c: 1, d: 3, e: 200}); // 200
91 max(['foo', 'lorem', 'amet'], function(val){
92     return val.length;
93 }); // 'lorem'
94 ```
95
96
97
98 ## min(list, [iterator]):*
99
100 Returns minimum value inside collection or use a custom iterator to define how
101 items should be compared.
102
103 See: [`max()`](#max), [array/min](array.html#min), [object/min](object.html#min)
104
105 ```js
106 min([10, 2, 7]); // 2
107 min({a: 'foo', b: 'lorem', c: 'amet'}, function(val){
108     return val.length;
109 }); // 'foo'
110 ```
111
112
113
114 ## pluck(list, propName):Array
115
116 Extract a list of property values.
117
118 ```js
119 var users = [
120     {
121         name : 'John',
122         age : 21
123     },
124     {
125         name : 'Jane',
126         age : 27
127     }
128 ];
129
130 pluck(users, 'name'); // ["John", "Jane"]
131 pluck(users, 'age'); // [21, 27]
132
133 users = {
134     first: {
135         name : 'John',
136         age : 21
137     },
138     second: {
139         name : 'Mary',
140         age : 25
141     }
142 };
143
144 pluck(users, 'name'); // ['John', 'Mary']
145 ```
146
147 See: [array/pluck](array.html#pluck), [object/pluck](object.html#pluck)
148
149
150
151 ## reduce(list, callback, initial, [thisObj]):*
152
153 Apply a function against an accumulator and each value in the collection as to
154 reduce it to a single value.
155
156 ```js
157 var obj = {a: 1, b: 2, c: 3, d: 4};
158
159 function sum(prev, cur, key, list) {
160     return prev + cur;
161 }
162
163 reduce(obj, sum); // 10
164 ```
165
166 See: [array/reduce](array.html#reduce), [object/reduce](object.html#reduce)
167
168
169
170 ## reject(list, fn, [thisObj]):Array
171
172 Creates a new array with all the elements that do **not** pass the truth test.
173 Opposite of [`filter()`](#filter).
174
175 ### Example
176
177 ```js
178 var numbers = [1, 2, 3, 4, 5];
179 reject(numbers, function(x) { return (x % 2) !== 0; }); // [2, 4]
180
181 var obj = {a: 1, b: 2, c: 3, d: 4, e: 5};
182 reject(obj, function(x) { return (x % 2) !== 0; }); // [2, 4]
183 ```
184
185 See: [array/reject](array.html#reject), [object/reject](object.html#reject)
186
187
188
189 ## size(list):Number
190
191 Returns the number of values in the collection.
192
193 ```js
194 var obj = {
195     foo : 1,
196     bar : 2,
197     lorem : 3
198 };
199 size(obj);     // 3
200 size([1,2,3]); // 3
201 size(null);    // 0
202 ```
203
204 See: [object/size](object.html#size)
205
206
207
208 ## some(list, callback, [thisObj]):Boolean
209
210 Tests whether any values in the collection pass the test implemented by the
211 provided callback.
212
213 ```js
214 var obj = {
215     a: 1,
216     b: 2,
217     c: 3,
218     d: 'string'
219 };
220
221 some(obj, isNumber);      // true
222 some(obj, isString);      // true
223 some([1, 2, 3], isNumber) // true
224 some([1, 2, 3], isString) // false
225 ```
226
227 See: [array/some](array.html#some), [object/some](object.html#some)
228
229
230 -------------------------------------------------------------------------------
231
232 For more usage examples check specs inside `/tests` folder. Unit tests are the
233 best documentation you can get...