Flatten JavaScript objects into a single-depth Object Last Updated : 14 Oct, 2025 Comments Improve Suggest changes 5 Likes Like Report Given a nested JavaScript object, the task is to flatten the object and pull out all the values to a single depth. If the values are already at a single depth then it returns the result unaltered.[Approach]: Using typeof()The typeof() method is used in the script to check the type of JavaScript variable.The Variable parameter represents the input value to be checked, and the method returns a string indicating the type of that variable.How it works:1. We make a function called flatten object which takes input of an object and returns an object.2. Loop through the object and check the type of the current property:If it is of type Object and it is not an Array, recursively call the function again.Otherwise, store the value in the result.3. Return the object. JavaScript // Declare an object let ob = { Company: "GeeksforGeeks", Address: "Noida", contact: +91-999999999, mentor: { HTML: "GFG", CSS: "GFG", JavaScript: "GFG" } }; // Declare a flatten function that takes // object as parameter and returns the // flatten object const flattenObj = (ob) => { // The object which contains the // final result let result = {}; // loop through the object "ob" for (const i in ob) { // We check the type of the i using // typeof() function and recursively // call the function again if ((typeof ob[i]) === 'object' && !Array.isArray(ob[i])) { const temp = flattenObj(ob[i]); for (const j in temp) { // Store temp in result result[i + '.' + j] = temp[j]; } } // Else store ob[i] in result directly else { result[i] = ob[i]; } } return result; }; console.log(flattenObj(ob)); Output{ Company: 'GeeksforGeeks', Address: 'Noida', contact: -999999908, 'mentor.HTML': 'GFG', 'mentor.CSS': 'GFG', 'mentor.JavaScript': 'GFG' } Create Quiz Comment S shubhamx Follow 5 Improve S shubhamx Follow 5 Improve Article Tags : JavaScript Web Technologies javascript-object JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like