What would be the best way to apply the bitwise OR operator (or any operator I suppose) to an array of values in javascript?
var array = [1, 5, 18, 4];
// evaluate 1 | 5 | 18 | 4
What would be the best way to apply the bitwise OR operator (or any operator I suppose) to an array of values in javascript?
var array = [1, 5, 18, 4];
// evaluate 1 | 5 | 18 | 4
Share
Improve this question
asked Oct 30, 2015 at 13:49
bflemi3bflemi3
6,79021 gold badges95 silver badges158 bronze badges
0
2 Answers
Reset to default 14Use reduce() and pass 0 as the initial value and logical or each value
var array = [1, 5, 18, 4];
var result = array.reduce(function(a, b) {
return a | b;
}, 0);
console.log(result);
simply use
array.reduce((a, b) => a | b, 0);