The push() function is used to add a new value to the end of the collection. In JavaScript, the array is first converted to a collection and then the function is applied to the collection.
Syntax:
data.push(value)
Parameters: This function accepts a single parameter as mentioned above and described below:
- value: This parameter holds the value to be added.
Return Value: Returns a modified collection which is created by this function.
Below examples illustrate the push() function in collect.js
Example 1:
Javascript
// It is used to import collect.js library const collect = require( 'collect.js' ); const collection = collect([ "Geeks" , "for" , "Geeks" ]); collection.push( "CS" ); console.log(collection.all()); |
Output:
[ 'Geeks', 'for', 'Geeks', 'CS' ]
Example 2:
Javascript
// It is used to import collect.js library const collect = require( 'collect.js' ); const collection = collect([10 , 20 , 30]); collection.push(40); console.log(collection.all()); |
Output:
[ 10, 20, 30, 40 ]
Reference: https://collect.js.org/api/push.html