Skip to content

Instantly share code, notes, and snippets.

@Akifcan
Created April 23, 2022 21:18
Show Gist options
  • Save Akifcan/2f662052e6ab1a33dee4edcabe4e1af3 to your computer and use it in GitHub Desktop.
Save Akifcan/2f662052e6ab1a33dee4edcabe4e1af3 to your computer and use it in GitHub Desktop.
Object flatten Example
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