Collect.js pad() Method
Last Updated :
30 Nov, 2020
The pad() method is used to fill the array with the given element until the size of the array is full. We use a negative size to pad the elements left side and use a positive sign to pad elements right side. If the array size is full then no padding will perform.
Syntax:
collect(array).pad(size, value)
Parameters: The collect() method takes one argument that is converted into the collection and then the pad() method is applied to it. The pad() method holds two parameters, the first one is the size and the other is the value.
Return Value: This method returns an array list with padded elements.
Module Installation: Install collect.js module using the following command from the root directory of your project:
npm install collect.js
The below example illustrates the pad() method in collect.js:
Example 1: Filename: index.js
Javascript
const collect = require( 'collect.js' );
const collection = collect([ 'Geeks' , 'GFG' , 'GeeksforGeeks' ]);
const pad_val = collection.pad(5, 'Welcome' );
console.log(pad_val.all());
|
Run the index.js file using the following command:
node index.js
Output:
[ 'Geeks', 'GFG', 'GeeksforGeeks', { G: 'Welcome' }, { G: 'Welcome' } ]
Example 2: Filename: index.js
Javascript
const collect = require( 'collect.js' );
const collection = collect([ 'Geeks' , 'GFG' , 'GeeksforGeeks' ]);
const pad_val = collection.pad(-5, 'Welcome' );
console.log(pad_val.all());
|
Run the index.js file using the following command:
node index.js
Output:
[ 'Welcome', 'Welcome', 'Geeks', 'GFG', 'GeeksforGeeks' ]
Share your thoughts in the comments
Please Login to comment...