How to cartesian product of 2 arrays using JavaScript ? Last Updated : 25 Jul, 2023 Comments Improve Suggest changes Like Article Like Report The task is to compute the cartesian product of two JavaScript arrays with the help of JavaScript. Here are a few techniques discussed. Approach 1: Create a new array.Traverse the first array by the outer loop and the second array by the inner loop.In the inner loop, Concatenate the first array element with the second array element and push it into a new array.Example: This example implements the above approach. JavaScript const arr1 = [ [13, 'G'], [16, 'C'] ]; const arr2 = [ [8, 'F'], [36, 'P'] ]; let res = ""; function CartesianProduct() { let ans = []; for (let i = 0; i < arr1.length; i++) { for (let j = 0; j < arr2.length; j++) { ans.push(arr1[i].concat(arr2[j])); } } res = ""; for (let i = 0; i < ans.length; i++) { res = res + "[" + ans[i] + "]\n"; } console.log(res); } CartesianProduct(); Output[13,G,8,F] [13,G,36,P] [16,C,8,F] [16,C,36,P]Approach 2: Create a new array.The same approach is followed here, for every element of the first array, every element of the second array is concatenated and pushed to the new array with the help of apply() and map() methods.Example: This example implements the above approach. JavaScript const arr1 = [ [13, 'G'], [16, 'C'] ]; const arr2 = [ [8, 'F'], [36, 'P'] ]; let res = ""; function CartesianProduct() { let ans = [].concat.apply([], arr1.map( arr1 => (arr2.map(arr2 => arr1.concat(arr2))))); res = ""; for (let i = 0; i < ans.length; i++) { res = res + "[" + ans[i] + "]\n"; } console.log(res); } CartesianProduct(); Output[13,G,8,F] [13,G,36,P] [16,C,8,F] [16,C,36,P] Comment More infoAdvertise with us Next Article How to cartesian product of 2 arrays using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Merge/Combine Arrays using JavaScript? Given two or more arrays, the task is to merge (or combine) arrays to make a single array in JavaScript. The simplest method to merge two or more arrays is by using array.concat() method.Using Array concat() MethodThe contact() method concatenates (joins) two or more arrays. It creates a new array a 2 min read Sum and Product of Array elements using JavaScript Given an array and is the task to find the Sum and Product of the values of an Array using JavaScript. Below are the approaches to find the sum and product of array elements using JavaScript:Table of ContentUsing Iterative methodUsing reduce() methodUsing Iterative methodIt uses a simple method to a 3 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 Product of an Array in JS or JavaScript We will be given an initial array and we have to multiply all the elements of the array with each other to find a final product.Examples: Input: arr = [1, 2, 3, 4, 5];Output: 120Explanation: 1 * 2 * 3 * 4 * 5 = 120Input: arr = [11, 12, 13, 14, 15]Output: 360360Explanation: 11 * 12 * 13 * 14 * 15 = 3 4 min read How to Create an Array using Intersection of two Arrays in JavaScript ? In this article, we will try to understand how to create an array using the intersection values of two arrays in JavaScript using some coding examples. For Example: arr1 = [1, 3, 5, 7, 9, 10, 14, 15]arr2 = [1, 2, 3, 7, 10, 11, 13, 14]result_arr = [1, 3, 7, 10, 14]Approach 1: We will see the native a 3 min read How to Create Zip Array in JavaScript ? Working with arrays in JavaScript often requires merging or combining multiple arrays into a single structure. One common operation in array manipulation is known as "zipping," where corresponding elements from multiple arrays are paired together to create a new array. Example:Input: let a = [1, 2, 3 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 JavaScript - Create an Object From Two Arrays Here are the different methods to create an object from two arrays in JavaScript1. Using for-each loopThe arr.forEach() method calls the provided function once for each element of the array. JavaScriptconst a1 = ['name', 'age', 'city']; const a2 = ['Ajay', 25, 'New Delhi']; const res = {}; a1.forEac 3 min read How to compare two JavaScript array objects using jQuery/JavaScript ? In this article, we are given two JavaScript array/array objects and the task is to compare the equality of both array objects. These are the methods to compare two JavaScript array objects: Using jQuery not() methodUse the sort() functionUse JSON.stringify() functionUsing every() and indexOf()Using 3 min read How to Convert Object to Array in JavaScript? In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such 4 min read Like