Check If All Values In An Object Are True In JavaScript

Check if all Values in an Object are True in JavaScript

We will learn how to check if all values in an object are true in JavaScript with two methods: Using filter() with Object.values() and Using Object.values() with every(). Each approach is relatively straightforward, so you should choose whatever you want.

How to check if all values in an object are true in JavaScript

Using filter() with Object.values()

If you want to check if all values in an object are true in JavaScript, you can filter out all the object values that equals to true. You can call this method on an array of the object values and pass the checking function to its argument.

Syntax:

filter(function)

Parameter:

  • function: write the function directly that you want to sort out.

You cannot call this method directly on an object. Instead, you will need to use Object.values() to get an values’ array and then use that array to call this method to filter them by true values.

First, you need to see how to convert the object into an array of values and the result after converting:

// Create an object
var object = {learn: "learnshareit.com", share: true, it: 1}

// Convert the object into array of values
var array = Object.values(object)
console.log(array)

Result: 

['learnshareit.com', true, 1]

The example below declared a new object to test the Object.value() function. As you can see, the result after converting is an array of the objects’ values. After that, we will use the filter() method and apply a checking function to sort out values that are true. If what you want is the boolean value true instead of a truthy value, use the strict equal operator (===) in the checking function as follows:

// Get all array elements that are equal to "true" boolean
let trues = array.filter(v => v === true)
console.log(trues)

Output: 

[true]

As can be seen, the array has been filled with true values. We will check if the length of this array is equal to the first array and if yes, it means all values in an object are true:

// Check if all values in object are true
if (array.length == trues.length)
	console.log("All values in object are true")
else console.log("Not all values in object are true")

Result: 

Not all values in object are true

If what you want is checking for truthy values instead of true booleans only. Then you should use the Boolean function or an equal operator (==) instead of strict equals. For example:

// Get all array elements that are equal to "true" 
let trues = array.filter(v => v == true)
console.log(trues)

// Check if all values in object are true
if (array.length == trues.length)
	console.log("All values in object are true")
else console.log("Not all values in object are true")

 Result: 

[true, 1]
Not all values in object are true

Another example:

// Get all array elements that are truthy values
let trues = array.filter(Boolean)
console.log(trues)

// Check if all values in object are true
if (array.length == trues.length)
	console.log("All values in object are true")
else console.log("Not all values in object are true")

 The output is much more different: 

['learnshareit.com', true, 1]
All values in object are true

Have you seen the differences between the three approaches of checking true values in the examples above? Depending on what you actually want, you can choose the approach that fits you best. To check for truthy values, use the Boolean function, to check for true numbers and true booleans but strings, use the equal operator instead. And to check for true booleans only, use strict equal (===) in the filter function.

Using Object.values() with every()

If you think that the above solution is long for you, you can use this function instead. The syntax and usage of this function is as follow:

Syntax:

every(function)

Parameter:

  • function: write the function directly that you want to check.

As we have explained clearly in the previous solution, there are three different ways to use this approach as it depends on what are true values you want:

// Check if all values in object are true booleans
if (array.every(v=>v===true))
	console.log("All values in object are true")
else console.log("Not all values in object are true")

Output: 

Not all values in object are true
// Check if all values in object are true
if (array.every(v=>v==true))
	console.log("All values in object are true")
else console.log("Not all values in object are true")

Result: 

Not all values in object are true
// Check if all values in object are truthy values
if (array.every(Boolean))
	console.log("All values in object are true")
else console.log("Not all values in object are true")

Output: 

All values in object are true

As you can see, this method cannot help you print out all values in objects that are true, and so you cannot know the logic behind the checking function. This is also considered the most effective way to check if all values in an object are true. However, we suggest you read the first solution as it will help you to decide what kind of true values you are looking for.

Summary

We have shown you how to check if all values in an object are true in JavaScript. It would help if you considered that the second method is the quickest way, but you should learn the logic in the first one before using it. We wish you success with our tutorials.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *