Underscore.js _.cat() Method

The _.cat() method is used to concatenate zero or more arguments into one array.
Syntax:
_.cat(arg1, arg2, ...)
Parameters:
- Args: This method accepts n arguments to concatenate into a single array.
Return Value: This method returns a concatenated array.
Note: This method will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.
underscore.js contrib library can be installed using npm install underscore-contrib –save
Example 1: In this example, we will concatenate 2 arrays.
Javascript
| // Defining underscore contrib variable var_ = require('underscore-contrib');   // Array1 vararr1 = [1,2,3]  // Array2 vararr2 = [4,5,6]  // Concatenation vararr = _.cat(arr1, arr2);  console.log("array 1 : "+ arr1);  console.log("array 2 : "+ arr2);  console.log("concatenated array : "+ arr);   | 
Output:
array 1 : 1,2,3 array 2 : 4,5,6 concatenated array : 1,2,3,4,5,6
Example 2: In this example, we will concatenate 2 numbers to form an array.
Javascript
| // Defining underscore contrib variable var_ = require('underscore-contrib');   // Number 1 varnum1 = 1  // Number 2 varnum2 = 4  // Concatenation vararr = _.cat(num1, num2);  console.log("num1 : "+ num1);  console.log("num2 : "+ num2);  console.log("concatenated array : "+ arr);   | 
Output:
num1 : 1 num2 : 4 concatenated array : 1,4
Example 3: In this example, we will concatenate 3 arrays.
Javascript
| // Defining underscore contrib variable var_ = require('underscore-contrib');  // Array1 vararr1 = [1,2,3] // Array2 vararr2 = [4,5,6] // Array3 vararr3 = [7,8,9] // Concatenation vararr = _.cat(arr1, arr2, arr3);  console.log("array 1 : "+ arr1);  console.log("array 2 : "+ arr2);  console.log("array 3 : "+ arr3); console.log("concatenated array : "+ arr);   | 
Output:
array 1 : 1,2,3 array 2 : 4,5,6 array 3 : 7,8,9 concatenated array : 1,2,3,4,5,6,7,8,9
Example 4: The _.cat() method will also work with the arguments object as if it were an array.
Javascript
| // Defining underscore contrib variable var_ = require('underscore-contrib');   // Function functionf(){ return_.cat(arguments, 4, 5, 6); }  console.log("Array is : "+ f(1,2,3)); | 
Output:
Array is : 1,2,3,4,5,6
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek 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!
 
				 
					


