user3060126
user3060126

Reputation: 501

JavaScript/jQuery check that array contains all a certain value?

Using JavaScript or jQuery, I want to check through an array to make sure all items have a certain value. So for example, if I am checking for "active" in the following arrays, I want:

["active"] => true
["active", "active", "active"] => true
["active", "pending", "active", "active"] => false
["pending"] => false

What's the simplest way to accomplish this?

Upvotes: 2

Views: 1786

Answers (1)

Javier Conde
Javier Conde

Reputation: 2583

You can use the JS Array function called every:

array.every(function(x) { return x == "active"; });

This will return true only if every element in array equals active.

Upvotes: 7

Related Questions