JS Object:
var saver = {
title: false,
preview: false,
body: false,
bottom: false,
locale: false
};
The question is how to check if all values is false?
I can use $.each() jQuery function and some flag variable, but there may be a better solution?
If you want to do it without external iteration (i.e. in your code), try mapping the properties to an array with
$.map
then using$.inArray
to see if any true values exist:JSFiddle: http://jsfiddle.net/TrueBlueAussie/FLhZL/1/
In a comment you ask if you can avoid iteration. You can if you use a javascript library supporting a functional approach, like Underscore, Lodash or Sugar.
With Underscore and Lodash you can write something like this:
With Sugar you can simply write:
With lodash you could also do
const result = _.some(saver);
Do like this,
As of Lodash 4.0, overEvery can be used
overEvery(saver, false)
loops through every element & checks if itsfalse
It returns
true
if every element isfalse
otherwise returnsfalse
This is a very simple solution that requires JavaScript 1.8.5.
Examples:
Alternatively you could write
See the Mozilla Developer Network Object.keys()'s reference for further information.