Created
February 9, 2025 22:51
-
-
Save Razza2018/3424992624c90270d4c97765fe1b869f to your computer and use it in GitHub Desktop.
Flatten object into key:value pair.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function flatten(data) { | |
let output = {}; | |
if (!Array.isArray(data) && (!data || typeof data !== 'object' || data.constructor !== Object)) return data; | |
for (const key in data) { | |
if (Array.isArray(data[key]) || (data[key] && typeof data[key] === 'object' && data[key].constructor === Object)) { | |
let returnData = flatten(data[key]); | |
for (let returnKey in returnData) { | |
output[key + '.' + returnKey] = returnData[returnKey]; | |
} | |
} else { | |
output[key] = data[key]; | |
} | |
} | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment