
- 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
Merging boolean array with AND operator - JavaScript
Let’s say, 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
Following is the code −
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
This will produce the following output in console −
[ false, false, false ]
- Related Articles
- JavaScript merge multiple Boolean arrays with the OR || operator
- Merging and rectifying arrays in JavaScript
- Merging nested arrays to form 1-d array in JavaScript
- Merging two sorted arrays into one sorted array using JavaScript
- Merging subarrays in JavaScript
- Alternatively merging two arrays - JavaScript
- Merging sorted arrays together JavaScript
- How to initialize a boolean array in JavaScript?
- Is there a Boolean Typed Array in JavaScript?
- How to reduce an array while merging one of its field as well in JavaScript
- Merging duplicate values into multi-dimensional array in PHP
- JavaScript Boolean constructor Property
- Boolean Gates in JavaScript
- Update a specific MongoDB document in array with $set and positional $ operator?
- Merging two arrays in a unique way in JavaScript

Advertisements