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

Lodash _.weave() Method

The Lodash _.weave() method takes two arrays and returns an array with both arrays woven together.

Note: The array returned from _.weave() method will be woven till the shortest array, rest array values will be the same.

Syntax:

_.weave(array1, array2)

Parameters: This method takes two parameters as mentioned above and described below:

  • array1: The first array.
  • array2: The second array.

Return Value: This method returns a woven array made from array1 and array2.

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

Module Installation: Lodash contrib library can be installed using the following command:

npm install lodash-contrib –save

Example 1: In this example, we will generate a woven array with array1’s and array2’s sizes as same.




// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
// Array1
var array1 = [1, 2, 8, 9];
  
// Array2
var array2 = [5, 6, 7, 8]
  
// Generating Array using weave() method
var arr =_.weave(array1, array2);
  
console.log("Array1 : ", array1);
console.log("Array2 : ", array2);
console.log("Woven Array : ", arr);


Output:

Array1 :  [ 1, 2, 8, 9 ]
Array2 :  [ 5, 6, 7, 8 ]
Woven Array :  [
  1, 5, 2, 6,
  8, 7, 9, 8
]

Example 2: When the size of array1 < size of array2




// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
// Array1
var array1 = [1, 2];
  
// Array2
var array2 = [5, 6, 7, 8]
  
// Generating Array using weave() method
var arr =_.weave(array1, array2);
  
console.log("Array1 : ", array1);
console.log("Array2 : ", array2);
console.log("Woven Array : ", arr);


Output:

Array1 :  [ 1, 2 ]
Array2 :  [ 5, 6, 7, 8 ]
Woven Array :  [ 1, 5, 2, 6, 7, 8 ]

Example 3: When the size of array1 > size of array2 




// Defining lodash contrib variable 
var _ = require('lodash-contrib'); 
  
// Array1
var array1 = [1, 2, 3, 4];
  
// Array2
var array2 = [5, 6]
  
// Generating Array using weave() method
var arr =_.weave(array1, array2);
  
console.log("Array1 : ", array1);
console.log("Array2 : ", array2);
console.log("Woven Array : ", arr);


Output:

Array1 :  [ 1, 2, 3, 4 ]
Array2 :  [ 5, 6 ]
Woven Array :  [ 1, 5, 2, 6, 3, 4 ]
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