Open In App

Collect.js pad() Method

Last Updated : 30 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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




// Requiring the module
const collect = require('collect.js');
  
// Creating collection object
const collection = collect(['Geeks', 'GFG', 'GeeksforGeeks']);
  
// Function call
const pad_val = collection.pad(5, 'Welcome');
  
// Printing values
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




// Requiring the module
const collect = require('collect.js');
  
// Creating collection object
const collection = collect(['Geeks', 'GFG', 'GeeksforGeeks']);
  
// Function call
const pad_val = collection.pad(-5, 'Welcome');
  
// Printing values
console.log(pad_val.all());


Run the index.js file using the following command:

node index.js

Output:

[ 'Welcome', 'Welcome', 'Geeks', 'GFG', 'GeeksforGeeks' ]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads