Reputation: 7862
I have hash that looks like this:
{ .user_import_163="100%", .user_import_164="99%"}
How in Javascript I can check that all values are equal to 100%?
Upvotes: 1
Views: 1172
Reputation: 1073968
How in Javascript I can check that all values are equal to 100%?
Assuming a valid JavaScript object:
var obj = {
user_import_163: "100%",
user_import_164: "99%"
};
or
var obj = {
".user_import_163": "100%",
".user_import_164": "99%"
};
You can do this:
if (Object.keys(obj).some(function(k) { return obj[k] !== "100%"; })) {
// At least one isn't equal to "100%"
} else {
// All are equal to "100%"
}
Live Example:
snippet.log("(true = at least one property is NOT 100%)");
test(1,
{
user_import_163: "100%",
user_import_164: "99%"
},
true
);
test(2,
{
user_import_163: "100%",
user_import_164: "100%"
},
false
);
function test(num, obj, expectedResult) {
var result = Object.keys(obj).some(function(k) {
return obj[k] !== "100%";
});
snippet.log(
"Test #" + num +
": Got " + result +
", expected " + expectedResult + " <== " +
(!result === !expectedResult ? "good" : "ERROR")
);
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Upvotes: 3
Reputation: 1434
Another sample:
var data = { a: '100%', b: '99%' }
var check = function(obj) {
var correct = true;
for (key in obj) {
if (obj[key] != '100%') correct = false;
}
return correct;
}
check(data); // return false
check({ a: '100%', b: '100%' }); // return true
Upvotes: 4