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

Shortest way to "or" a list of booleans in javascript - Stack Overflow

programmeradmin5浏览0评论

I have a list of booleans. I want to apply || ("or") on each of them, and get the result. (So, a new boolean.)

The list has no fixed length, so I can't simply write the code as value = l[0] || ... l[length-1]. (It would also be ugly for long lists.)

My approach:

let index = 0;
let value = true;
while(index < list.length && value == value || list[index]) {
    index += 1;
}

I guess that this is possible as one statement. How?

I have a list of booleans. I want to apply || ("or") on each of them, and get the result. (So, a new boolean.)

The list has no fixed length, so I can't simply write the code as value = l[0] || ... l[length-1]. (It would also be ugly for long lists.)

My approach:

let index = 0;
let value = true;
while(index < list.length && value == value || list[index]) {
    index += 1;
}

I guess that this is possible as one statement. How?

Share Improve this question edited Dec 31, 2019 at 18:45 Ry- 225k56 gold badges490 silver badges497 bronze badges asked Dec 31, 2019 at 18:40 AsqiirAsqiir 6271 gold badge8 silver badges24 bronze badges 5
  • Does the code work as currently written? – Robert Harvey Commented Dec 31, 2019 at 18:44
  • 2 Are you aware of Array.prototype.some? developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Nicholas Tower Commented Dec 31, 2019 at 18:44
  • 1 Applying an 'or' to each of the indexes of your list of booleans is the same as checking if any of them are actually true, so you could literally just see if the list has the value 'true' in it, using Array.includes(true) – oriont Commented Dec 31, 2019 at 18:48
  • "or" is cheap, when used on a large bunch. (Very fast.) How expensive are those functions? – Asqiir Commented Dec 31, 2019 at 18:51
  • @Asqiir: Not very. They’re just function calls plus very similar loops, and JavaScript engines are decent at function calls. If you profile and this turns out to be a bottleneck, you can pare them. (I expect includes to be the fastest and about as good as a loop.) – Ry- Commented Dec 31, 2019 at 18:52
Add a ment  | 

4 Answers 4

Reset to default 14

You could use .includes, which checks if an Array includes a value, and returns true / false

const list = [true, true, true, true, false];

// Checks if list contains the Boolean True
const value = list.includes(true);

console.log(value);

One option is to use .some:

const value = list.some(Boolean);

const list = [false, false, false, true, false];
const value = list.some(Boolean);
console.log(value);

const list = [false, false, false, false, false];
const value = list.some(Boolean);
console.log(value);

This is a classic use case for reduce:

value = list.reduce((a, b) => a || b, false);

(Note the starting accumulator value should be false - using true as you are, the result will always be true!)

In the general case, this is exactly what Array.prototype.reduce is for:

list.reduce((previousValue, currentValue) => previousValue || currentValue)

reduce (or fold as it is called in some other languages) is a general method of iteration which means that anything you can do with a loop, you can also do with reduce. What reduce does, is use a binary operation to "fold" all elements of a collection into a new value. In this case, it is a rather simple operation, and the result type is the same type as the elements, but that doesn't have to be the case: the result type can be different than the element type, and the result type can be arbitrarily plex (it can even be again a collection).

You can think of reduce as replacing the ma in an array with a binary operator. In other words, if you have

const arr = [a, b, c, d, e, f, g];

then

arr.reduce((accumulator, element) => accumulator + element)

will pute

a + b + c + d + e + f + g

However, because reduce is a general method, it doesn't convey much meaning. There are more specialized methods that cannot do "everything", but using those methods tells the reader more about what is going on in your code. So, for example, you can use reduce to transform each element, but you should rather use Array.prototype.map for that.

Note that your specific example has some specific properties: since OR-ing a bunch of values together is only false if every single value is false, or in other words, is true if at least one value is true, all we need to check is if there is some element that is true.

And there is a specific method for checking whether some element has a specific property: Array.prototype.some:

list.some(el => el)

In this case, the property is actually just the identity function. Alternatively, you can use the Boolean constructor, which acts as the identity function for booleans:

list.some(Boolean)

you can also use Array.prototype.includes to check if the list includes at least one true value:

list.includes(true)
发布评论

评论列表(0)

  1. 暂无评论