The forPage() function is used to return the value which is present at a particular page, this function take the page number as an argument and then returns the collection. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.
Syntax:
data.forPage(x,y)
Parameters: This function accept two parameter as mentioned above and described below :
- x: This parameter holds the page number.
- y: This parameter holds the number of items to show per page.
Return Value: Returns a new collection.
Below examples illustrate the forPage() function in collect.js
Example 1: Here in this example, we take a collection and then using the forPage() function we display the value from the page given as argument, and return the value
Javascript
// It is used to import collect.js library const collect = require( 'collect.js' ); const collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]); const Page= collection.forPage(2, 5); console.log(Page.all()); |
Output:
[ 6, 7, 8, 9 ]
Example 2:
Javascript
// It is used to import collect.js library const collect = require( 'collect.js' ); const collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]); const Page= collection.forPage(4, 2); console.log(Page.all()); |
Output:
[ 7, 8 ]
Reference: https://collect.js.org/api/forPage.html