最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Apply bitwise operator to values of array - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 14

Use 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);
发布评论

评论列表(0)

  1. 暂无评论