
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
AND product of arrays in JavaScript
We have an array of arrays of boolean like this −
const arr = [[true,false,false],[false,false,false],[false,false,true]];
We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator.
Let’s write the code for this function. We will be using Array.prototype.reduce() function to achieve this.
Example
The code for this will be −
const arr = [[true,false,false],[false,false,false],[false,false,true]]; const andMerge = (arr = []) => { return arr.reduce((acc, val) => { val.forEach((bool, ind) => { acc[ind] = acc[ind] && bool || false; }); return acc; }, []); }; console.log(andMerge(arr));
Output
The output in the console will be −
[ false, false, false ]
- Related Articles
- Cartesian product of multiple arrays in JavaScript
- Dash separated cartesian product of any number of arrays in JavaScript
- Matrix product of two arrays in Numpy
- Get the Kronecker product of two arrays in Python
- Get the Outer product of two arrays in Python
- Get the Inner product of two arrays in Python
- Get the Kronecker product of arrays with 4D and 3D dimensions in Python
- Comparing and filling arrays in JavaScript
- Merging and rectifying arrays in JavaScript
- Get the Outer product of two multidimensional arrays in Python
- Return the inner product of two masked arrays in Numpy
- Return the outer product of two masked arrays in Numpy
- Return the dot product of two masked arrays in Numpy
- Return the cross product of two (arrays of) vectors in Python
- Product of all pairwise consecutive elements in an Arrays in C++

Advertisements