How to check if every property on object is the same recursively in JavaScript?

When working with nested objects in JavaScript, you might need to check if all leaf values (final non-object values) are identical. This requires a recursive approach to traverse through all nested levels and compare the actual values at the end of each branch.

For example, in this object:

const obj = {
   a: 1,
   b: 1,
   c: {
      aa: 1
   }
};

The function should return true because all leaf values equal 1, even though one is nested inside object c.

Recursive Solution

Here's a function that recursively checks all leaf values in a nested object:

const obj = {
   a: 1,
   b: 1,
   c: {
      aa: 3
   }
};

const allSame = (obj, value) => {
   const keys = Object.keys(obj);
   for(let i = 0; i < keys.length; i++){
      if(typeof obj[keys[i]] === "object" && 
      !Array.isArray(obj[keys[i]])){
         return allSame(obj[keys[i]], value);
      };
      if(!value){
         value = obj[keys[i]];
         continue;
      }
      if(obj[keys[i]] !== value){
         return false;
      };
   };
   return true;
}

console.log(allSame(obj));
console.log(allSame({ a: 1, b: 1, c: { aa: 1 } }));
console.log(allSame({ a: { x: 1 }, b: 1, c: { aa: 1 } }));
console.log(allSame({ a: 1, b: 1, c: { aa: 2 } }));
false
true
true
false

How It Works

The function works by:

  • Iterating through object keys: Uses Object.keys() to get all property names
  • Checking for nested objects: If a value is an object (but not an array), it recursively calls itself
  • Setting reference value: The first leaf value encountered becomes the comparison baseline
  • Comparing values: All subsequent leaf values are compared against the reference

Improved Version with Better Error Handling

const allLeafValuesSame = (obj) => {
   let referenceValue = undefined;
   let hasValue = false;
   
   function traverse(current) {
      for (const key in current) {
         const value = current[key];
         
         // Handle nested objects (but not arrays or null)
         if (typeof value === "object" && value !== null && !Array.isArray(value)) {
            if (!traverse(value)) return false;
         } else {
            // This is a leaf value
            if (!hasValue) {
               referenceValue = value;
               hasValue = true;
            } else if (value !== referenceValue) {
               return false;
            }
         }
      }
      return true;
   }
   
   return traverse(obj);
};

// Test cases
console.log(allLeafValuesSame({ a: 1, b: 1, c: { aa: 1 } })); // true
console.log(allLeafValuesSame({ a: 1, b: 1, c: { aa: 2 } })); // false
console.log(allLeafValuesSame({ a: { x: { y: 5 } }, b: 5 })); // true
console.log(allLeafValuesSame({})); // true (empty object)
true
false
true
true

Key Points

  • Recursive traversal: The function explores nested objects until it finds leaf values
  • Type checking: Uses typeof and additional checks to identify objects vs. primitives
  • Array handling: Arrays are treated as leaf values, not traversed further
  • Null safety: The improved version handles null values properly

Conclusion

This recursive approach effectively compares all leaf values in nested objects. The improved version provides better error handling and cleaner logic for complex nested structures.

Updated on: 2026-03-15T23:18:59+05:30

501 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements