Collect.js concat() Method

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); 
 
// concat 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); 
 
// concat the array
let concatarr = collection.concat(['a', 'b', 'c']);
 
// concat the object
concatarr = concatarr.concat({ first : "w3wiki",
        second : "Collect.js"});
     
// Returning the array
let newObject =  concatarr.all(); 
     
console.log("Result : ", newObject);


Output:

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

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



Contact Us