File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -168,6 +168,7 @@ Following API examples are shown based on the sample JSON data given [here](exam
168
168
* [ sortBy] ( #sortbyproperty-order )
169
169
* [ reset] ( #resetdata )
170
170
* [ copy] ( #copy )
171
+ * [ chunk] ( #chunk )
171
172
172
173
### ` fetch() `
173
174
@@ -523,6 +524,12 @@ It will return a complete clone of the Object instance.
523
524
524
525
See a detail example [ here] ( examples/copy.js ) .
525
526
527
+ ### ` chunk(size) `
528
+
529
+ It will return a complete new array after chunking your array with specific size.
530
+
531
+ See a detail example [ here] ( examples/chunk.js ) .
532
+
526
533
## Bugs and Issues
527
534
528
535
If you encounter any bugs or issues, feel free to [ open an issue at
Original file line number Diff line number Diff line change
1
+ /*
2
+ Example of Chunk() API
3
+ ---------------------
4
+ You need to pass an integer number to chunk method to get your desired result.
5
+ */
6
+
7
+ const jsonQ = require ( '../index.js' ) ;
8
+ let Q = new jsonQ ( __dirname + '/data.json' ) ;
9
+
10
+ const chunking = Q . from ( 'users' )
11
+ . where ( 'location' , '=' , 'Barisal' )
12
+ . chunk ( 4 ) ;
13
+
14
+ console . log ( '-------- Printing Result of chunk ---------' ) ;
15
+ console . log ( chunking ) ;
Original file line number Diff line number Diff line change @@ -80,6 +80,32 @@ class JSJsonQ {
80
80
return this . _jsonContent ;
81
81
}
82
82
83
+ /**
84
+ * chunk - group the resulted collection to multiple chunk
85
+ *
86
+ * @param {integer } The length of each chunk
87
+ * @return {Array } New Array
88
+ */
89
+
90
+ chunk ( size = 0 ) {
91
+ if ( size <= 0 ) {
92
+ throw Error ( 'Invalid chunk size' ) ;
93
+ }
94
+
95
+ this . _prepare ( ) ;
96
+
97
+ let _newContent = [ ] ;
98
+
99
+ while ( this . _jsonContent . count ( ) > 0 ) {
100
+ _newContent . push ( this . _jsonContent . splice ( 0 , size ) ) ;
101
+ }
102
+
103
+ this . _jsonContent = _newContent ;
104
+
105
+ return this . _jsonContent ;
106
+ }
107
+
108
+
83
109
/**
84
110
* at - get the data traversing through the given path hierarchy
85
111
* Will return the JsonQuery Object instance, so that further
You can’t perform that action at this time.
0 commit comments