Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Collect.js put() Method

  • Last Updated : 30 Nov, 2020

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

Syntax:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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




onst 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
}
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!