
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to check if every property on object is the same recursively in JavaScript?
Let’s say, we are required to write a function, say isSame() that accepts a nested object and returns a boolean depending on the fact whether or not all the keys have the same values. When saying all the keys we mean all the last keys like if a key has a nested object as its value, we are required to traverse to the end of the nested object and check for that value.
For example − If the object is −
const obj = { a: 1, b: 1, c: { aa: 1 } };
Then the function should return true as all the end keys have the same value (1). Therefore, let’s write a recursive solution to this problem.
Example
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 } }));
Output
The output in the console will be −
false true true false
- Related Articles
- Can we check if a property is in an object with JavaScript?
- How to check if the constructor of an object is a JavaScript Object?
- How to check if a value is object-like in JavaScript?
- How to check if CSS property is supported in the browser using JavaScript?
- How to check if an object is empty using JavaScript?
- How to check if CSS property is supported in browser using JavaScript?
- Recursively flat an object JavaScript
- How to sort a JavaScript object list based on a property when the property is not consistent
- Recursively list nested object keys JavaScript
- Group values on same property - JavaScript
- How do we check if an object is an array in Javascript?
- How to check if an object is an instance of a Class in JavaScript?
- Check if an array is growing by the same margin in JavaScript
- JavaScript: How to Check if a String is a Literal or an Object?
- Grouping on the basis of object property JavaScript

Advertisements