Skip to content
Related Articles

Related Articles

Collect.js concat() Method
  • Last Updated : 29 Jul, 2020

The concat() method is used to return the merged arrays or objects represented by the collection. The JavaScript array is first transformed into a collection and then the function is applied to the collection.

Syntax:

collect(array1).concat(array2)

Parameters: The collect() method takes one argument that is converted into the collection and then concat() function also take an array.

Return Value: Returns a merged array or object.

Below examples illustrate the concat() method in Collect.js:



Example 1: Here collect = require(‘collect.js’) is used to import the collect.js library into the file.

Javascript




const collect = require('collect.js');  
      
let arr = [1, 2, 3]  
      
// Convert array into collection  
const collection = collect(arr);  
  
// concate the array
let concatarr = collection.concat(['a', 'b', 'c']);
      
// Returning the array 
let newObject =  concatarr.all();  
      
console.log("Result : ", newObject);

Output:

Result :  [ 1, 2, 3, 'a', 'b', 'c' ]

Example 2:

Javascript




const collect = require('collect.js');  
      
let arr = [1, 2, 3]  
      
// Convert array into collection  
const collection = collect(arr);  
  
// concate the array
let concatarr = collection.concat(['a', 'b', 'c']);
  
// concate the object
concatarr = concatarr.concat({ first : "GeeksforGeeks"
        second : "Collect.js"});
      
// Returning the array 
let newObject =  concatarr.all();  
      
console.log("Result : ", newObject);

Output:

Result :  [ 1, 2, 3, 'a', 'b', 'c', 'GeeksforGeeks', 'Collect.js' ]

Reference: https://collect.js.org/api/concat.html

My Personal Notes arrow_drop_up
Recommended Articles
Page :