Reputation: 501
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
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