
- 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
JavaScript merge multiple Boolean arrays with the OR || operator
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 OR (||) operator.
Let’s write the code for this function. We will be using Array.prototype.reduce() function to achieve this.
Example
const arr = [[true,false,false],[false,false,false],[false,false,true]]; const orMerge = arr => { return arr.reduce((acc, val) => { val.forEach((bool, ind) => acc[ind] = acc[ind] || bool); return acc; }, []); }; console.log(orMerge(arr));
Output
The output in the console will be −
[ true, false, true ]
- Related Articles
- Merging boolean array with AND operator - JavaScript
- Merge two arrays with alternating Values in JavaScript
- Select multiple values with MongoDB OR operator
- How to merge two arrays with objects in one in JavaScript?
- How to merge two arrays in JavaScript?
- Spread operator for arrays in JavaScript
- JavaScript - Merge two arrays according to id property
- How do I concatenate or Merge Arrays in Swift?
- MySQL query to return multiple row records with AND & OR operator
- How to use spread operator to join two or more arrays in JavaScript?
- How to merge an array with an object where values are arrays - JavaScript
- Finding intersection of multiple arrays - JavaScript
- Compute the bit-wise OR of two boolean arrays element-wise in Numpy
- Merge arrays in column wise to another array in JavaScript
- Cartesian product of multiple arrays in JavaScript

Advertisements