Saturday, October 25, 2025
HomeLanguagesJavascriptLodash _.interpose() Method

Lodash _.interpose() Method

The Lodash _.interpose() Method takes an array and an element and returns a new array with the given element inserted in between every element of the original array.

Syntax:

_.interpose(array, element)

Parameters: This method takes two parameters as shown above and discussed below:

  • array: The array in which element is to be inserted.
  • element: The element to be inserted between every other element.

Return Value: This method returns a newly created interposed array.

Note: This will not work in normal JavaScript because it requires the lodash contrib library to be installed.

lodash contrib library can be installed using npm install lodash-contrib –save

Example 1: In this example, we will create a new array using this method.

Javascript




// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Array
var arr = [8,8,8,8,8,8];
  
// Element
var ele = 0
  
// Constructing interposed array
var i_arr = _.interpose(arr, ele);
  
console.log("Array : ");
console.log(arr);
console.log("Element : ");
console.log(ele);
console.log("Interposed array : ");
console.log(i_arr);


Output:

Array :
[ 8, 8, 8, 8, 8, 8 ]
Element :
0
Interposed array :
[
  8, 0, 8, 0, 8,
  0, 8, 0, 8, 0,
  8
]

Example 2: If there are no between exist, then the original array is returned.

Javascript




// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Array
var arr = [8];
  
// Element
var ele = 0
  
// Constructing interposed array
var i_arr = _.interpose(arr, ele);
  
console.log("Array : ");
console.log(arr);
console.log("Element : ");
console.log(ele);
console.log("Interposed array : ");
console.log(i_arr);


Output: Here, the chunk array is compensated due to the shortage of elements.

Array :
[ 8 ]
Element :
0
Interposed array :
[ 8 ]

Example 3: For empty array, the same empty array is returned.

Javascript




// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
   
// Array
var arr = [];
  
// Element
var ele = 0
  
// Constructing interposed array
var i_arr = _.interpose(arr, ele);
  
console.log("Array : ");
console.log(arr);
console.log("Element : ");
console.log(ele);
console.log("Interposed array : ");
console.log(i_arr);


Output: Here, the chunk array is compensated due to the shortage of elements.

Array :
[ 0 ]
Element :
0
Interposed array :
[ 0 ]

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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS