How to create an array of elements, ungrouping the elements in an array produced by zip in JavaScript ? Last Updated : 21 Dec, 2022 Comments Improve Suggest changes Like Article Like Report We can create an array of elements by ungrouping the elements in an array produced by zip by using different methods in JavaScript such as Math.max(), array specific methods namely Array.prototype.map(), Array.prototype.reduce() and Array.prototype.forEach(). This operation can also be termed as unzip. Before looking at unzip, let us understand what zip does. Zip creates an array of elements, grouped based on their position in the original arrays. To achieve this, we can make use of JavaScript methods such as Math.max(), Array.from(), and Array.prototype.map(). Approach: Math.max(): To get the longest array out of all argument arrays.Then, create an array with that length as a return value and use Array.from() with a mapping function to create an array of grouped elements.Array.prototype.map(): To convert every array element into a new array.Moreover, the spread operator (...) is used to represent a variable number of arrays that can be passed as arguments in the parameter field of the zip function. Example: This example demonstrates the above-explained approach. JavaScript <script> const zip = (...arrays) => { const maxLength = Math.max( ...arrays.map(arr => arr.length)); return Array.from({ length: maxLength }) .map((_, i) => { return Array.from( {length: arrays.length }, (_, j) => arrays[j][i]); }); }; console.log( zip( [1, "GFG", 0.1], [2, "GeeksForGeeks", 0.2], [3, "Geeks", 0.3], [4, "For", 0.4], [5, "Geeks", 0.5] ) ); </script> Output: Now let us look at how we can implement unzip: Math.max(): Get the longest subarray in the array.Array.prototype.map(): Make each array element into an array.Array.prototype.reduce() and Array.prototype.forEach(): Map the grouped values to individual arrays.Lastly, Array.prototype.map() and the spread operator (...) are used to apply the f parameter in the unzip function to each individual group of elements. Example: This example demonstrates the above-explained approach. JavaScript <script> const unzip = (arr, f) => arr.reduce( (a, myValue) => (myValue.forEach( (val, i) => a[i].push(val)), a), Array.from({ length: Math.max( ...arr.map(myArr => myArr.length)), }).map(myArr => []) ) .map(v => f(...v)); console.log( unzip( [ [1, 3, 5], [2, 6, 10], [3, 9, 15], ], (...arguments) => arguments .reduce((acc, j) => acc + j, 0) ) ); </script> Output: [3,6,18] Comment More infoAdvertise with us Next Article How to create an array of elements, ungrouping the elements in an array produced by zip in JavaScript ? R rajatsandhu2001 Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads How to create a string by joining the elements of an array in JavaScript ? Given an array containing array elements and here we will join all the array elements to make a single string. To join the array elements we use arr.join() method. There are two methods by which we can create a string by joining the elements of an array: Table of Content Using arr.join() MethodUsing 2 min read How to Convert Array into Array of Objects using map() & reduce() in JavaScript? An array of objects can contain multiple objects with the same properties i.e. key-value pairs. But a map does not contain duplicate values which means if you convert an array of objects with duplicate objects into a map, it will contain only the unique key-value pairs. Below are the approaches to c 2 min read How to Create Nested Arrays from a Nest of Arrays in JavaScript ? Creating nested arrays from a nest of arrays in JavaScript involves organizing multiple arrays into a hierarchical structure, which is often useful for managing and representing complex data relationships. This process entails encapsulating arrays within other arrays to form multi-dimensional struct 3 min read How to filter out the non-unique values in an array using JavaScript ? In JavaScript, arrays are the object using the index as the key of values. In this article, let us see how we can filter out all the non-unique values and in return get all the unique and non-repeating elements. These are the following ways by which we can filter out the non-unique values in an arra 4 min read How to Merge/Flatten an array of arrays in JavaScript ? Merging or flattening an array of arrays in JavaScript involves combining multiple nested arrays into a single-level array. This process involves iterating through each nested array and appending its elements to a new array, resulting in a flattened structure.To Merge or flatten array we can use arr 3 min read How to Flatten a Given Array up to Specified Depth in JavaScript? Flatten an array in JavaScript is a common task when dealing with nested arrays. The concept of "flatten" means to convert a multi-dimensional array into a single-dimensional array. When you need to control the depth of flattening, JavaScript provides methods to handle this efficiently. This process 2 min read How to Remove duplicate elements from array in JavaScript ? In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index 4 min read How to unpack array elements into separate variables using JavaScript ? Given an array and the task is to unpack the value of the array into separate variables by using the preferred method using javascript.We can unpack the array elements into separate variables by the following methods:Table of ContentDestructuring AssignmentUsing Array.slice() MethodUsing the spread 2 min read How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app 5 min read JavaScript Program to Print All Distinct Elements in an Integer Array Given an Array of Integers consisting of n elements with repeated elements, the task is to print all the distinct elements of the array using JavaScript. We can get the distinct elements by creating a set from the integer array. Examples: Input : arr = [ 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9 ] Outpu 3 min read Like