@@ -78,8 +78,10 @@ For the remainder of this documentation, we'll discuss each method available on
78
78
[ map] ( #method-map )
79
79
[ mapWithKeys] ( #method-mapwithkeys )
80
80
[ max] ( #method-max )
81
+ [ median] ( #method-median )
81
82
[ merge] ( #method-merge )
82
83
[ min] ( #method-min )
84
+ [ mode] ( #method-mode )
83
85
[ nth] ( #method-nth )
84
86
[ only] ( #method-only )
85
87
[ partition] ( #method-partition )
@@ -147,22 +149,15 @@ The `all` method returns the underlying array represented by the collection:
147
149
<a name =" method-avg " ></a >
148
150
#### ` avg() ` {#collection-method}
149
151
150
- The ` avg ` method returns the average of all items in the collection :
152
+ The ` avg ` method returns the [ average value ] ( https://en.wikipedia.org/wiki/Average ) of a given key :
151
153
152
- collect([1, 2, 3, 4, 5] )->avg();
154
+ $average = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]] )->avg('foo' );
153
155
154
- // 3
155
-
156
- If the collection contains nested arrays or objects, you should pass a key to use for determining which values to calculate the average:
157
-
158
- $collection = collect([
159
- ['name' => 'JavaScript: The Good Parts', 'pages' => 176],
160
- ['name' => 'JavaScript: The Definitive Guide', 'pages' => 1096],
161
- ]);
156
+ // 20
162
157
163
- $collection ->avg('pages' );
158
+ $average = collect([1, 1, 2, 4]) ->avg();
164
159
165
- // 636
160
+ // 2
166
161
167
162
<a name =" method-chunk " ></a >
168
163
#### ` chunk() ` {#collection-method}
@@ -737,6 +732,19 @@ The `max` method returns the maximum value of a given key:
737
732
738
733
// 5
739
734
735
+ <a name =" method-median " ></a >
736
+ #### ` median() ` {#collection-method}
737
+
738
+ The ` median ` method returns the [ median value] ( https://en.wikipedia.org/wiki/Median ) of a given key:
739
+
740
+ $median = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->median('foo');
741
+
742
+ // 15
743
+
744
+ $median = collect([1, 1, 2, 4])->median();
745
+
746
+ // 1.5
747
+
740
748
<a name =" method-merge " ></a >
741
749
#### ` merge() ` {#collection-method}
742
750
@@ -773,6 +781,19 @@ The `min` method returns the minimum value of a given key:
773
781
774
782
// 1
775
783
784
+ <a name =" method-mode " ></a >
785
+ #### ` mode() ` {#collection-method}
786
+
787
+ The ` mode ` method returns the [ mode value] ( https://en.wikipedia.org/wiki/Mode_(statistics) ) of a given key:
788
+
789
+ $mode = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->mode('foo');
790
+
791
+ // [10]
792
+
793
+ $mode = collect([1, 1, 2, 4])->mode();
794
+
795
+ // [1]
796
+
776
797
<a name =" method-nth " ></a >
777
798
#### ` nth() ` {#collection-method}
778
799
0 commit comments