Open In App

Collect.js put() Method

Last Updated : 16 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The put() method is used to set the given key and value in the collection.

Syntax:

collect(array).put(key)

Parameters: The collect() method takes one argument that is converted into the collection and then put() method is applied on it. The put() method holds the key as parameter.

Below example illustrate the put() method in collect.js:

Example 1:

Javascript




const collect = require('collect.js');
 
let obj = ['Geeks', 'GFG', 'GeeksforGeeks'];
 
const collection = collect(obj);
 
collection.put('Welcome');
 
console.log(collection.all());


Output:

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

Example 2:

Javascript




const collect = require('collect.js');
 
let obj = {
    name: 'Rahul',
    dob: '25-10-96',
    section: 'A',
    score: 98,
};
 
const collection = collect(obj);
 
collection.put(['Welcome to GeeksforGeeks']);
 
console.log(collection.all());


Output:

{
  name: 'Rahul',
  dob: '25-10-96',
  section: 'A',
  score: 98,
  'Welcome to GeeksforGeeks': undefined
}

Similar Reads

Collect.js toArray() Method
The toArray() method is used to convert the given collection into a normal array. An array containing the values will be returned if the collection is an object. Syntax: collect(array).toArray() Parameters: The collect() method takes one argument that is converted into the collection and the toArray() method is applied to it. Return Value: This met
1 min read
Collect.js replace() Method
The replace() method is used to replace the elements of the original collection that match the given string or numeric keys. If the key given in the object matches with a key in the collection, then its value is replaced, otherwise, if the key does not match, then it is added to the collection. Syntax: collect(array).replace(object) Parameters: The
1 min read
Collect.js reject() Method
The reject() method is used to filter the given collection of elements using the given callback function. If the callback function returns true, then the element is removed from the resulting collection, otherwise, it is not removed. Syntax: collect(array).reject(callback) Parameters: The collect() method takes one argument that is converted into t
1 min read
Collect.js | avg() Method
Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection. The avg() method returns the average of all the items in a collection. Installation: Collect.js can be installed via NPM: npm install --save collect.js You
2 min read
Collect.js | combine() Method
Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection.The combine() method combines two different arrays with keys-values. Installation: Collect.js can be installed via NPM: npm install --save collect.jsYou can
1 min read
Collect.js | crossJoin() Method
Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection. The crossJoin() method cross joins the collection with an array or collection and return all possible permutations between them. Installation: Collect.js c
1 min read
Collect.js | collapse() Method
Collect.js is a fluent and convenient wrapper for working with arrays and objects. The JavaScript array is first transformed into a collection and then the function is applied to the collection. The collapse() method convert collection of arrays into a single flat array. Installation: Collect.js can be installed via NPM: npm install --save collect.
2 min read
Collect.js | all() Method
The all() method is used to return the underlying array or object represented by the collection. The JavaScript array is first transformed into a collection and then the function is applied to the collection. Syntax: collect(array).all() Parameters: The method does not accept any parameter. Return Value: Returns an array or object. Below examples i
1 min read
Collect.js map() Method
The map() function iterates through a collection and pass each value to callback.In JavaScript, the array is first converted to a collection and then the function is applied to the collection.Syntax: data.map(rule) Parameters: This function accepts a single parameter as mentioned above and described below: rule: This parameter holds the operation r
1 min read
Collect.js each() Method
The each() function iterates over the items in the collection and passes each item to a callback. In JavaScript, the array is first converted to a collection and then the function is applied to the collection. Syntax: data.each(item) Parameters: This function accept a single parameter as mentioned above and described below: item: This parameter hol
1 min read