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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Array1
var arr1 = [1,2,3]
  
// Array2
var arr2 = [4,5,6]
  
// Concatenation
var arr = _.cat(arr1, arr2); 
console.log("array 1 : "+ arr1); 
console.log("array 2 : "+ arr2); 
console.log("concatenated array : "+ arr); 

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Number 1
var num1 = 1
  
// Number 2
var num2 = 4
  
// Concatenation
var arr = _.cat(num1, num2); 
console.log("num1 : "+ num1); 
console.log("num2 : "+ num2); 
console.log("concatenated array : "+ arr); 

chevron_right


Output:

num1 : 1
num2 : 4
concatenated array : 1,4

Example 3: In this example, we will concatenate 3 arrays.

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
// Array1
var arr1 = [1,2,3]
// Array2
var arr2 = [4,5,6]
// Array3
var arr3 = [7,8,9]
// Concatenation
var arr = _.cat(arr1, arr2, arr3); 
console.log("array 1 : "+ arr1); 
console.log("array 2 : "+ arr2); 
console.log("array 3 : "+ arr3);
console.log("concatenated array : "+ arr); 

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable
var _ = require('underscore-contrib'); 
  
// Function
function f(){ return _.cat(arguments, 4, 5, 6); }
  
console.log("Array is : " + f(1,2,3));

chevron_right


Output:

Array is : 1,2,3,4,5,6

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.