Thursday, November 13, 2025
HomeLanguagesJavascriptCollect.js merge() Method

Collect.js merge() Method

The merge() method is used to merge the given object into the original collection. If the key of a given object is the same as the collection object then it overwrites the value of the key.

Syntax:

collect(array).merge(object)

Parameters: The collect() method takes one argument that is converted into the collection and then the merge() method is applied to it. The merge() method holds the object as a parameter.

Return Value: This method returns the collection of merged 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 merge() method in collect.js:

Example 1: Filename: index.js

Javascript




// Requiring the module
const collect = require('collect.js');
  
let obj = ['Geeks', 'neveropen'];
  
// Function call
const collection = collect(obj);
  
const merged_val = collection.merge(['Welcome', 'GFG']);
  
// Printing the merged collection
console.log(merged_val.all());


Run the index.js file using the following command:

node index.js

Output:

[ 'Geeks', 'neveropen', 'Welcome', 'GFG' ]

Example 2: Filename: index.js

Javascript




// Requiring the module
const collect = require('collect.js');
  
let obj = [
    {
        name: 'Rahul',
        dob: '25-10-96',
    },
    {
        name: 'Aditya',
        dob: '25-10-96',
    }
];
  
// Function call
const collection = collect(obj);
  
const merged_val = collection.merge({
    address: 'Noida',
    school: 'neveropen',
});
  
// Printing the merged collection
console.log(merged_val.all());


Run the index.js file using the following command:

node index.js

Output:

[
  { name: 'Rahul', dob: '25-10-96' },
  { name: 'Aditya', dob: '25-10-96' },
  address: 'Noida',
  school: 'neveropen'
]
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6839 POSTS0 COMMENTS