Created
April 23, 2022 21:18
-
-
Save Akifcan/2f662052e6ab1a33dee4edcabe4e1af3 to your computer and use it in GitHub Desktop.
Object flatten Example
This file contains hidden or 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
const cv = { | |
age: 20, | |
name: 'akif', | |
title: 'web developer', | |
contact: { | |
number: '0535-232-22-17' | |
}, | |
address: { | |
home: 'bornova', | |
scholl: { | |
name: 'yaşar', | |
department: 'computer programming', | |
location: { | |
name: 'izmir', | |
city: { | |
name: { | |
neighbourhood: 'ergene' | |
} | |
} | |
} | |
} | |
} | |
} | |
const flatten = {} | |
function flat(obj, name='') { | |
Object.keys(obj).forEach(key => { | |
if (typeof obj[key] === 'object') { | |
flat(obj[key],name+key+'.') | |
} else { | |
flatten[name+key] = obj[key] | |
} | |
}) | |
return flatten | |
} | |
console.log(flat(cv)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment